Arithmetic operations

val=`expr $a + $b`

Operators

SymbolDescriptionExample
!NOT[ ! false ]
-oOR[ $a -lt 20 -o $b -gt 20 ]
-aAND[ $a -lt 20 -a $b -gt 20 ]
=equality check[ $a = $b ]
!=inequality check[ $a != $b ]
-zstring length is 0, returns true if 0[ -z $a ]
-nstring length is not 0, returns true if not 0[ -n $a ]
strcheck whether string is empty, true if not[ $a ]
-bcheck whether file is a block device[ -b $file ]
-ccheck whether file is a character device..
-dcheck whether file is a directory[ -d $file ]
-fcheck whether file is a regular file[ -f $file ]
-rcheck whether file is readable..
-wcheck whether file is writable..
-xcheck whether file is executable..
-scheck whether file is empty..
-echeck whether file exists..

Special variables

VariableMeaning
$0file name of the current script
$narguments passed to a script or function; n is the position
$#number of arguments passed to a script or function
$*all arguments as a single word, e.g., “1 2 3”
$@all arguments as separate words, e.g., “1” “2” “3”
$?exit status of the last command or return value of a function
$$current shell process ID; for scripts, the PID of the script process

POSIX program exit statuses

CodeMeaning
0command exited successfully
> 0failure during redirection or word expansion (~, variables, commands, arithmetic, and word splitting)
1 - 125command exited unsuccessfully; specific meanings are command-defined
126command found but file is not executable
127command not found
> 128command died from a signal

Input/output redirection

CommandDescription
command > fileredirect output to file
command > fileredirect output to file by appending
n > fileredirect file descriptor n to file
n » fileredirect file descriptor n to file by appending
n >& mmerge output file m and n
n <& mmerge input file m and n
« taguse content between start tag and end tag as input

File include

Use . or source to include files.

. filename
source filename

Included files do not need execute permissions.

Using functions

Basic function structure

# Use the function name directly; optionally prefix with function
function_name(){

    # Use $#,$*,$@ and $0-$n to get the function name and other parameters
    val="$1"

    # Optional return value (must be an integer status code)
    # To return results, use global variables
    return $val
}

Function invocation

# Call the function name directly, space-separated arguments are allowed
function_name 1 2 3 4
# Use $? to get the return value
ret=$?

Common snippets

List all files in a directory

The code below lists all xlsx files in the downloads directory. Note that to list all files, use "$watch_dir"/*.

watch_dir="/Users/mylxsw/Downloads"
# Avoid returning a * when there is no match
shopt -s nullglob
for file in "$watch_dir"/*.xlsx
do
    echo $file
done

for file in "$watch_dir"/*/    # List all directories
do
    echo $file
done

See How to get the list of files in a directory in a shell script? and Bash Shell Loop Over Set of Files

Read lines from stdin

while read line
do
    if [ $line != 'EOF' ]
    then
        echo $line
    fi
done

Date and time

Get current date

# Output: 20161023
echo $(date +%Y%m%d)

Time comparison

Convert to UNIX timestamps, then compare directly.

time1=`date +%s`
time2=`date -d '2016-10-25 17:20:13' +%s`

if [ $time1 -gt $time2 ]; then
    echo "time1 > time2"
else
    echo "time1 < time2"
fi