Professional Documents
Culture Documents
Bob Myhill
Abstract
A few magic bash tips and tricks that could be very useful!!
1 General Tips
1. Ctrl-R allows an interactive search of previous commands sent to the
Terminal.
2. The header #!/bin/bash -x allows the user to see which bit of the
script is running during execution.
3. Commenting can be done per line by using the hash symbol, or per
block, by using the following trick:
:<<’COMMENT_NAME’
Commenting goes here
COMMENT_NAME
2 Variable manipulation
1. Variables can be nested (using quotations where spaces are required,
thus:
1
This can become very useful at times such as making a backup, moving
files, comparing files ... almost anything really! For example:
This is much easier to nest than our other substitution friend the
backtick (‘).
2
3 Functions
1. We declare functions like so:
function() {
Operations on ${var_a} ($1) and ${var_a} ($2) go here
}
function ${var_a} ${var_b}
4 awk
1. The simplest way to use external variables in awk is to copy them to
an internal variable:
5 grep
1. One can grep many items at the same time using |: