15. Quick Reference

This is a quick reference guide to the meaning of some of the less easily guessed commands and codes of shell scripts. By their nature, they are also quite difficult to find using search engines. These examples include process management, shell scripts arguments, and shell script test conditions.

CommandDescriptionExample
&Run the previous command in the backgroundls &
&&Logical ANDif [ "$foo" -ge "0" ] && [ "$foo" -le "9"]
||Logical ORif [ "$foo" -lt "0" ] || [ "$foo" -gt "9" ] (not in Bourne shell)
^Start of linegrep "^foo"
$End of linegrep "foo$"
=String equality (cf. -eq)if [ "$foo" = "bar" ]
!Logical NOTif [ "$foo" != "bar" ]
$$PID of current shellecho "my PID = $$"
$!PID of last background commandls & echo "PID of ls = $!"
$?exit status of last commandls ; echo "ls returned code $?"
$0Name of current command (as called)echo "I am $0"
$1Name of current command's first parameterecho "My first argument is $1"
$9Name of current command's ninth parameterecho "My ninth argument is $9"
$@All of current command's parameters (preserving whitespace and quoting)echo "My arguments are $@"
$*All of current command's parameters (not preserving whitespace and quoting)echo "My arguments are $*"
-eqNumeric Equalityif [ "$foo" -eq "9" ]
-neNumeric Inqualityif [ "$foo" -ne "9" ]
-ltLess Thanif [ "$foo" -lt "9" ]
-leLess Than or Equalif [ "$foo" -le "9" ]
-gtGreater Thanif [ "$foo" -gt "9" ]
-geGreater Than or Equalif [ "$foo" -ge "9" ]
-zString is zero lengthif [ -z "$foo" ]
-nString is not zero lengthif [ -n "$foo" ]
-ntNewer Thanif [ "$file1" -nt "$file2" ]
-dIs a Directoryif [ -d /bin ]
-fIs a Fileif [ -f /bin/ls ]
-rIs a readable fileif [ -r /bin/ls ]
-wIs a writable fileif [ -w /bin/ls ]
-xIs an executable fileif [ -x /bin/ls ]
parenthesis:
( ... )
Function definitionfunction myfunc() { echo hello }
Hints and Tips Interactive Shell

Books and eBooks


Contact

You can mail me with this form. If you expect a reply, please ensure that the address you specify is valid.