#!/bin/sh

sh uses different syntax for operators than bash.

Use -o for or in place of || and -a for and in place of &&.

Shell Symbols
  • $1..$9 - arguments
  • $0 - process name
  • $# - number of arguments
  • $? - exit status
  • $$ - PID of this shell
  • $! - PID of the last background command
  • $- - option supplied at shell call (?)
  • $* - all arguments
  • $@ - all arguments with quotes
Conditional Statements
  • -a and (has higher precedence than -o)
  • -o or
String Operators
  • = or == equal to
  • -n string is not null
  • -z string is null, that is, has zero length
Numeric Operators
  • -eq number equals
  • -ne number does not equal
  • -gt greater than
  • -ge greater than or equal to
  • -lt less than
  • -le less than or equal to
File Operators
  • -e file exists
  • -f file is not a directory or device file
  • -d file exists and is a directory
  • -h file exists and is a symlink
  • -s file exists and has a size greater than zero
  • -w file exists and is writeable
  • -x file exists and is executable
  • -r file exists and is readable
  • -b file is a block device
  • -c file is a character device
  • -p file is a pipe
  • -S file is a socket
  • -O you are owner of file
  • -G you are group owner of file
Built-in commands
  • : - null command
  • . file - execute command in current shell
  • read - assign var from stdin
  • readonly - mark var as read only
  • trap - trap signals
  • type - inidicate how the shell interprets the command
  • exec - execute command in place of current shell without creating a new process
  • hash - rehash command table
Redirect stderr
echo test 2> /dev/null
Redirect stderr to stdout
echo test 2>&1

String Comparison

When checking for string length, include the string in quotes.

Both of these will return true:

[ -n string ]
[ -n ]

This will return false:

[ -n "" ]