You are on page 1of 3

Bash Tips and Tricks

Bob Myhill

February 10, 2010

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

4. Using /dev/tcp/ is amazing. It will create a TCP socket that is con-


nected to the named host on the given port. This lets you easily use
network sockets with regular shell IO redirection. You can read and
write to these sockets. For example:

cat < /dev/tcp/time.nist.gov/13 gets the current time

2 Variable manipulation
1. Variables can be nested (using quotations where spaces are required,
thus:

$ echo {one,two,red,blue}" fish"


one fish two fish red fish blue fish

1
This can become very useful at times such as making a backup, moving
files, comparing files ... almost anything really! For example:

$ cp filename{,.bak} is the same as $ cp filename filename.bak


$ mv filename{.dat,.txt,} is the same as $ mv filename.dat
filename.txt filename

2. We can create an array variable easily, like so:

array=(‘saclst EVDP DIST BAZ f *.BHZ‘)

We can use the first element by typing $array[0].

3. Command substitution is our friend. Enclose any command (or set


of nested commands) that produces standard output like so: $(com-
mand). For example, to list all the files from all the RPM packages
that have httpd in the name:

$ rpm -ql $(rpm -qa | grep httpd)

This is much easier to nest than our other substitution friend the
backtick (‘).

4. Redirecting output is easy:

find / -name foo > output.txt


directs standard output to file output.txt.
find / -name foo > output.txt 2> error.txt
directs standard output and error to different files.
find / -name foo > output.txt 2>&1
directs both standard output and standard error to output.txt.

5. Given foo=/tmp/my.dir/filename.tar.gz we can use the following ex-


pressions:

path = ${foo%/*} to get: /tmp/my.dir (like dirname)


file = ${foo##*/} to get: filename.tar.gz (like basename)
base = ${file%%.*} to get: filename
ext = ${file#*.} to get: tar.gz

6. The above tricks use the following bash operators:

${variable%pattern} Trim the shortest match from the end


${variable##pattern} Trim the longest match from the beginning
${variable%%pattern} Trim the longest match from the end
${variable#pattern} Trim the shortest match from the beginning

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}

2. Local variables can be defined within functions by prefixing the word


local. This way the same variable name can be used inside and outside
the function, and any manipulations inside the function on the local
variable do not change the value of the global variable.

3. Functions can be nested like so:

$(func_a $(func_b $1))

4 awk
1. The simplest way to use external variables in awk is to copy them to
an internal variable:

awk ’substr($1,1,3)==var_a’ var_a=$i file.dat

5 grep
1. One can grep many items at the same time using |:

egrep -i this|little|piggy file.dat

Any spaces must be specified as [[:space:]]

You might also like