BASH Tricks

The following are some bash usage examples. They are not organized because they are meant to be accessed by links from other documentation. Think of them as a collection of footnotes.


Command substitution.

`command` or $(command) substitutes the standard output of command. In the following example the process with the pid found in the file /var/run/ppp0.pid is killed if the process id was placed in the file.

    kill `cat /var/run/ppp0.pid`

The next example demonstrates how to substitute the current kernel version number into a path:

    /lib/modules/`uname -r`/modules.dep

The current kernel version is substituted for `uname -r` and the path to modules.dep is completed. The resulting path would look something like this:

    /lib/modules/2.2.16/modules.dep

(Note: ` and ' are different characters. The character ' will not work for command substitution.)


Function definitions

function name () { list; } or name () { list; } defines a function called name and will execute whatever is in list. If you want to define a function to view the contents of a tar ball called myfile.tar.gz type the following

    tart () { tar tzvf $1; }

at the command line; then type “tart myfile.tar.gz” and you will have a list of the contents of the tar ball. To have the function available all the time simply add the definition to your ~/.profile or ~/.bashrc file.

Last updated: