String Operators
  • -a and
  • -o or
  • = 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 (returns false in Bourne shell if file is a symlink)
  • -f file is not a directory or device file
  • -d file exists and is a directory
  • -h file exists and is a symlink (also -L)
  • -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
Regular Expressions

Use =~ followed by a regular expression pattern. Do not enclose the pattern in quotes if using in a test block.

Check if a variable is a positive integer:

[[ $1 =~ ^[0-9]+$ ]]

Functions are created and called the same as any shell script: use command line arguments as parameters to pass, and echo any output.

function parse_input() {
   echo "You passed ${1} as an argument"
Read each line
cat filename | while read line; do
  echo "Your line says ${line}"
String subset

Syntax: ${variable:<starting integer>[:<num characters>]}

string="O hai"
echo ${string:2} # returns hai

Get string length

expr length $string

Find the index of the first occurrence of a letter in a string

Returns 0 on not found.

expr index abcdefg d

Strings as commands

You need to use eval to reparse a command through bash.

See here

cmd="date --date=\"1 days ago\""
$cmd         # Doesn't work
eval $cmd    # Works

Simple sed replacement

echo testing testing2 | sed -e 's/ /\|/' # returns testing|testing2

Simple cut with a delimiter, grabbing a field

eix --only-names -Ie bash | cut -d "/" -f 2
  • $# - number of bash arguments
  • $* - display all variables passed
  • $$ - pid of shell, or script being run
  • $0 - name of the script
  • $! - pid of last job run in background
  • $_ - last command run, without the arguments
Displaying variables

When setting a variable using the backtick operator, use double quotes around the variable to retain newlines.

echo "$files"
Console settings

Get the size of the available cols, lines. Useful when writing shell applications that display output that needs to be paged:

tput cols
tput lines
Cool bash stuff
  • mktemp (coreutils) make a temporary file in /tmp
Get exit status from piped outputs

“Wonder piping!” –Shaine

$ true | false | true
$ echo ${PIPESTATUS[*]}
Special variables
  • $# Number of arguments on commandline.
  • $? Exit status of last command.
  • $$ Process id of current program.
  • $! Process id of last backgroundjob or background function.
  • $0 Program name including the path if started from another directory.
  • $1..n Commandline arguments, each at a time.
  • $* All commandline arguments in one string.
Color Normal Bold
Gray 00;38 01;30
Black 00;30
Red 00;31 01;31
Green 00;32 01;32
Purple 1 00;33 01;33
Blue 00;34 01;34
Purple 2 00;35 01;35
Cyan 00;36 01;36
White 00;37 01;38
Bright Blue 01;37
Yellow 02;33
Dark Gray
Trapping signals

You can use trap to capture a signal, such as Ctl-C to kill a command, and then map it to an internal function inside the bash script.

This is useful in scenarios where a shell script calls other scripts, and cancelling them would only cancel that one executed script instead of the main one.

control_c() {
        exit 1

trap control_c INT
Command Line Control
  • Delete: Ctrl W
  • Clear Line: Ctrl U
  • Start of Line: Ctrl A
  • End of Line: Ctrl E
Check Input

Check to see if input is an integer:

if [[ $1 == ?(-)+([0-9]) ]]; then echo "$1 is an integer"; fi