Bash quick reference

Command Description Example
& Run the previous command in the background ls &
&& Logical AND if [ “$foo” -ge “0” ] && [ “$foo” -le “9”]
|| Logical OR if [ “$foo” -lt “0” ] || [ “$foo” -gt “9” ] (not in Bourne shell)
^ Start of line grep ”^foo”
$ End of line grep “foo$”
= String equality (cf. -eq) if [ “$foo” = “bar” ]
! Logical NOT if [ “$foo” != “bar” ]
$$ PID of current shell echo “my PID = $$”
$! PID of last background command ls & echo “PID of ls = $!”
$? exit status of last command ls ; echo “ls returned code $?”
$0 Name of current command (as called) echo “I am $0”
$1 Name of current command's first parameter echo “My first argument is $1”
$9 Name of current command's ninth parameter echo “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 $*”
parenthesis: ( … ) Function definition function myfunc() { echo hello }
-b block special file if [ -b filename ]
-c Special character fileif [ -c filename ]
-d Is a Directory if [ -d /bin ]
-e Is file exist if [ -e /bin/ls ]
-eq Numeric Equality if [ “$foo” -eq “9” ]
-f Is a File if [ -f /bin/ls ]
-G check if file exists and is owned by effective groupid if [ -G /bin/ls ]
-g true if file exists and is set-group-id if [ -g /bin/ls ]
-ge Greater Than or Equal if [ “$foo” -ge “9” ]
-gt Greater Than if [ “$foo” -gt “9” ]
-k check sticky bit if [ -k /bin/ls ]
-L if file a symbolic link if [ -L /etc/alternatives/editor ]
-le Less Than or Equal if [ “$foo” -le “9” ]
-lt Less Than if [ “$foo” -lt “9” ]
-n String is not zero length if [ -n “$foo” ]
-ne Not Equal if [ “$foo” -ne “9” ]
-nt Newer Than if [ “$file1” -nt “$file2” ]
-O True if file exists and is owned by the effective user id if [ -O file ]
-r Is a readable file if [ -r /bin/ls ]
-s if file is nonzero size if [ -s /bin/ls ]
-S if file is a socket if [ -S file ]
-u check if file set-user-id bit is set if [ -u /bin/ls ]
-w Is a writable file if [ -w /bin/ls ]
-x Is an executable file if [ -x /bin/ls ]
-z String is zero length if [ -z “$foo” ]