Skip to content
English
On this page

Bash scripting cheatsheet

Introduction

Example

bash
#!/usr/bin/env bash

NAME="John"
echo "Hello $NAME!"

Variables

bash
NAME="John"
echo $NAME
echo "$NAME"
echo "${NAME}!"

String quotes

bash
NAME="John"
echo "Hi $NAME"  #=> Hi John
echo 'Hi $NAME'  #=> Hi $NAME

Shell execution

bash
echo "I'm in $(pwd)"
echo "I'm in `pwd`"
# Same

Conditional execution

bash
git commit && git push
git commit || echo "Commit failed"

Strict mode

bash
set -euo pipefail
IFS=$'\n\t'

Functions

bash
get_name() {
  echo "John"
}

echo "You are $(get_name)"

Conditionals

bash
if [[ -z "$string" ]]; then
  echo "String is empty"
elif [[ -n "$string" ]]; then
  echo "String is not empty"
fi

Brace expansion

bash
echo {A,B}.js

#=> {A,B}	Same as A B 
#=> {A,B}.js	Same as A.js B.js
#=> {1..5}	Same as 1 2 3 4 5

Parameter expansions

Basics

bash
name="John"
echo ${name}
echo ${name/J/j}    #=> "john" (substitution)
echo ${name:0:2}    #=> "Jo" (slicing)
echo ${name::2}     #=> "Jo" (slicing)
echo ${name::-1}    #=> "Joh" (slicing)
echo ${name:(-1)}   #=> "n" (slicing from right)
echo ${name:(-2):1} #=> "h" (slicing from right)
echo ${food:-Cake}  #=> $food or "Cake"

length=2
echo ${name:0:length}  #=> "Jo"

STR="/path/to/foo.cpp"
echo ${STR%.cpp}    # /path/to/foo
echo ${STR%.cpp}.o  # /path/to/foo.o
echo ${STR%/*}      # /path/to

echo ${STR##*.}     # cpp (extension)
echo ${STR##*/}     # foo.cpp (basepath)

echo ${STR#*/}      # path/to/foo.cpp
echo ${STR##*/}     # foo.cpp

echo ${STR/foo/bar} # /path/to/bar.cpp

STR="Hello world"
echo ${STR:6:5}   # "world"
echo ${STR: -5:5}  # "world"

SRC="/path/to/foo.cpp"
BASE=${SRC##*/}   #=> "foo.cpp" (basepath)
DIR=${SRC%$BASE}  #=> "/path/to/" (dirpath)

Comments

bash
# Single line comment
: '
This is a
multi line
comment
'

Substitution

bash
${FOO%suffix}	#=> Remove suffix
${FOO#prefix}	#=> Remove prefix
${FOO%%suffix}	#=> Remove long suffix
${FOO##prefix}	#=> Remove long prefix
${FOO/from/to}	#=> Replace first match
${FOO//from/to}	#=> Replace all
${FOO/%from/to}	#=> Replace suffix
${FOO/#from/to}	#=> Replace prefix

Length

bash
${#FOO}	#=> Length of $FOO

Default values

bash
${FOO:-val}	#=> $FOO, or val if unset (or null)
${FOO:=val}	#=> Set $FOO to val if unset (or null)
${FOO:+val}	#=> val if $FOO is set (and not null)
${FOO:?message}	#=> Show error message and exit if $FOO is unset (or null)

Substrings

bash
${FOO:0:3}	#=> Substring (position, length)
${FOO:(-3):3}	#=> Substring from the right

Manipulation

bash
STR="HELLO WORLD!"
echo ${STR,}   #=> "hELLO WORLD!" (lowercase 1st letter)
echo ${STR,,}  #=> "hello world!" (all lowercase)

STR="hello world!"
echo ${STR^}   #=> "Hello world!" (uppercase 1st letter)
echo ${STR^^}  #=> "HELLO WORLD!" (all uppercase)

Loops

Basic for loop

bash
for i in /etc/rc.*; do
  echo $i
done

Reading lines

bash
cat file.txt | while read line; do
  echo $line
done

C-like for loop

bash
for ((i = 0 ; i < 100 ; i++)); do
  echo $i
done

Forever

bash
while true; do
  ···
done

Ranges

bash
for i in {1..5}; do
    echo "Welcome $i"
done

#=> With step size

for i in {5..50..5}; do
    echo "Welcome $i"
done

Functions

Defining functions

bash
myfunc() {
    echo "hello $1"
}
# Same as above (alternate syntax)
function myfunc() {
    echo "hello $1"
}

Returning values

bash
myfunc() {
    local myresult='some value'
    echo $myresult
}
result="$(myfunc)"

Arguments

bash
$#	#=> Number of arguments
$*	#=> All arguments
$@	#=> All arguments, starting from first
$1	#=> First argument
$_	#=> Last argument of the previous command

Raising errors

bash
myfunc() {
  return 1
}
if myfunc; then
  echo "success"
else
  echo "failure"
fi

Conditionals

Conditions

Note that [[ is actually a command/program that returns either 0 (true) or 1 (false). Any program that obeys the same logic (like all base utils, such as grep(1) or ping(1)) can be used as condition, see examples.

bash
[[ -z STRING ]]	#=> Empty string
[[ -n STRING ]]	#=> Not empty string
[[ STRING == STRING ]]	#=> Equal
[[ STRING != STRING ]]	#=> Not Equal
[[ NUM -eq NUM ]]	#=> Equal
[[ NUM -ne NUM ]]	#=> Not equal
[[ NUM -lt NUM ]]	#=> Less than
[[ NUM -le NUM ]]	#=> Less than or equal
[[ NUM -gt NUM ]]	#=> Greater than
[[ NUM -ge NUM ]]	#=> Greater than or equal
[[ STRING =~ STRING ]]	#=> Regexp
(( NUM < NUM ))	#=> Numeric conditions
More conditions
[[ -o noclobber ]]	#=> If OPTIONNAME is enabled
[[ ! EXPR ]]	#=> Not
[[ X && Y ]]	#=> And
[[ X || Y ]]	#=> Or

File conditions

bash
[[ -e FILE ]]	#=> Exists
[[ -r FILE ]]	#=> Readable
[[ -h FILE ]]	#=> Symlink
[[ -d FILE ]]	#=> Directory
[[ -w FILE ]]	#=> Writable
[[ -s FILE ]]	#=> Size is > 0 bytes
[[ -f FILE ]]	#=> File
[[ -x FILE ]]	#=> Executable
[[ FILE1 -nt FILE2 ]]	#=> 1 is more recent than 2
[[ FILE1 -ot FILE2 ]]	#=> 2 is more recent than 1
[[ FILE1 -ef FILE2 ]]	#=> Same files

[[ -b FILE ]] #=> True if the FILE exists and is a block special file.
[[ -c FILE ]] #=> True if the FILE exists and is a special character file.
[[ -G FILE ]] #=> True if the FILE exists and has the same group as the user running the command.
[[ -h FILE ]] #=> True if the FILE exists and is a symbolic link.
[[ -g FILE ]] #=> True if the FILE exists and has set-group-id (sgid) flag set.
[[ -k FILE ]] #=> True if the FILE exists and has a sticky bit flag set.
[[ -L FILE ]] #=> True if the FILE exists and is a symbolic link.
[[ -O FILE ]] #=>True if the FILE exists and is owned by the user running the command.
[[ -p FILE ]] #=>True if the FILE exists and is a pipe.
[[ -S FILE ]] #=>True if the FILE exists and is socket.
[[ -u FILE ]] #=>True if the exists and set-user-id (suid) flag is set.

Example

bash
# String
if [[ -z "$string" ]]; then
  echo "String is empty"
elif [[ -n "$string" ]]; then
  echo "String is not empty"
else
  echo "This never happens"
fi

# Combinations
if [[ X && Y ]]; then
  ...
fi

# Equal
if [[ "$A" == "$B" ]]

# Regex
if [[ "A" =~ . ]]
if (( $a < $b )); then
   echo "$a is smaller than $b"
fi
if [[ -e "file.txt" ]]; then
  echo "file exists"
fi

Arrays

Defining arrays

bash
Fruits=('Apple' 'Banana' 'Orange')
Fruits[0]="Apple"
Fruits[1]="Banana"
Fruits[2]="Orange"

Operations

bash
Fruits=("${Fruits[@]}" "Watermelon")    # Push
Fruits+=('Watermelon')                  # Also Push
Fruits=( ${Fruits[@]/Ap*/} )            # Remove by regex match
unset Fruits[2]                         # Remove one item
Fruits=("${Fruits[@]}")                 # Duplicate
Fruits=("${Fruits[@]}" "${Veggies[@]}") # Concatenate
lines=(`cat "logfile"`)                 # Read from file

Working with arrays

bash
echo ${Fruits[0]}           # Element #0
echo ${Fruits[-1]}          # Last element
echo ${Fruits[@]}           # All elements, space-separated
echo ${#Fruits[@]}          # Number of elements
echo ${#Fruits}             # String length of the 1st element
echo ${#Fruits[3]}          # String length of the Nth element
echo ${Fruits[@]:3:2}       # Range (from position 3, length 2)
echo ${!Fruits[@]}          # Keys of all elements, space-separated

Iteration

bash
for i in "${arrayName[@]}"; do
  echo $i
done

Dictionaries

Defining

bash
sounds[dog]="bark"
sounds[cow]="moo"
sounds[bird]="tweet"
sounds[wolf]="howl"

Working with dictionaries

bash
echo ${sounds[dog]} # Dog's sound
echo ${sounds[@]}   # All values
echo ${!sounds[@]}  # All keys
echo ${#sounds[@]}  # Number of elements
unset sounds[dog]   # Delete dog

Iteration

bash
# Iterate over values
for val in "${sounds[@]}"; do
  echo $val
done

#Iterate over keys
for key in "${!sounds[@]}"; do
  echo $key
done

Options

Options

bash
set -o noclobber  # Avoid overlay files (echo "hi" > foo)
set -o errexit    # Used to exit upon error, avoiding cascading errors
set -o pipefail   # Unveils hidden failures
set -o nounset    # Exposes unset variables

Glob options

bash
shopt -s nullglob    # Non-matching globs are removed  ('*.foo' => '')
shopt -s failglob    # Non-matching globs throw errors
shopt -s nocaseglob  # Case insensitive globs
shopt -s dotglob     # Wildcards match dotfiles ("*.sh" => ".foo.sh")
shopt -s globstar    # Allow ** for recursive matches ('lib/**/*.rb' => 'lib/a/b/c.rb')

History

Commands

bash
history	#=>  Show history
shopt -s histverify	#=> Don’t execute expanded result immediately

Operations

bash
!!	#=> Execute last command again
!!:s/<FROM>/<TO>/	#=> Replace first occurrence of <FROM> to <TO> in most recent command
!!:gs/<FROM>/<TO>/	#=> Replace all occurrences of <FROM> to <TO> in most recent command
!$:t	#=> Expand only basename from last parameter of most recent command
!$:h	#=> Expand only directory from last parameter of most recent command

# !! and !$ can be replaced with any valid expansion.

Expansions

bash
!$	#=> Expand last parameter of most recent command
!*	#=> Expand all parameters of most recent command
!-n	#=> Expand nth most recent command
!n	#=> Expand nth command in history
!<command>	#=> Expand most recent invocation of command <command>

Slices

bash
!!:n	#=> Expand only nth token from most recent command (command is 0; first argument is 1)
!^	#=> Expand first argument from most recent command
!$	#=> Expand last token from most recent command
!!:n-m	#=> Expand range of tokens from most recent command
!!:n-$	#=> Expand nth token to last from most recent command
      	
# !! can be replaced with any valid expansion i.e. !cat, !-2, !42, etc.

Miscellaneous

Numeric calculations

bash
$((a + 200))      # Add 200 to $a
$(($RANDOM%200))  # Random number 0..199

Inspecting commands

bash
command -V cd
#=> "cd is a function/alias/whatever"

Trap errors

bash
trap 'echo Error at about $LINENO' ERR

# or

traperr() {
  echo "ERROR: ${BASH_SOURCE[1]} at about ${BASH_LINENO[0]}"
}

set -o errtrace
trap traperr ERR

Subshells

bash
(cd somedir; echo "I'm now in $PWD")
pwd # still in first directory

Redirection

bash
python hello.py > output.txt   # stdout to (file)
python hello.py >> output.txt  # stdout to (file), append
python hello.py 2> error.log   # stderr to (file)
python hello.py 2>&1           # stderr to stdout
python hello.py 2>/dev/null    # stderr to (null)
python hello.py &>/dev/null    # stdout and stderr to (null)
python hello.py < foo.txt      # feed foo.txt to stdin for python

Case/switch

bash
case "$1" in
  start | up)
    vagrant up
    ;;

  *)
    echo "Usage: $0 {start|stop|ssh}"
    ;;
esac

printf

bash
printf "Hello %s, I'm %s" Sven Olga
#=> "Hello Sven, I'm Olga

printf "1 + 1 = %d" 2
#=> "1 + 1 = 2"

printf "This is how you print a float: %f" 2
#=> "This is how you print a float: 2.000000"

Heredoc

bash
cat <<END
hello world
END

Reading input

bash
echo -n "Proceed? [y/n]: "
read ans
echo $ans
read -n 1 ans    # Just one character

Go to previous directory

bash
pwd # /home/user/foo
cd bar/
pwd # /home/user/foo/bar
cd -
pwd # /home/user/foo

Grep check

bash
if grep -q 'foo' ~/.bash_history; then
  echo "You appear to have typed 'foo' in the past"
fi

Date

bash
date +"%m-%d-%y" #=> 02-27-21

date +"%T" #=> 18:08:21

FORMAT control characters supported

%FORMAT StringDescription
%%a literal %
%alocale’s abbreviated weekday name (e.g., Sun)
%Alocale’s full weekday name (e.g., Sunday)
%blocale’s abbreviated month name (e.g., Jan)
%Blocale’s full month name (e.g., January)
%clocale’s date and time (e.g., Thu Mar 3 23:05:25 2005)
%Ccentury; like %Y, except omit last two digits (e.g., 21)
%dday of month (e.g, 01)
%Ddate; same as %m/%d/%y
%eday of month, space padded; same as %_d
%Ffull date; same as %Y-%m-%d
%glast two digits of year of ISO week number (see %G)
%Gyear of ISO week number (see %V); normally useful only with %V
%hsame as %b
%Hhour (00..23)
%Ihour (01..12)
%jday of year (001..366)
%khour ( 0..23)
%lhour ( 1..12)
%mmonth (01..12)
%Mminute (00..59)
%na newline
%Nnanoseconds (000000000..999999999)
%plocale’s equivalent of either AM or PM; blank if not known
%Plike %p, but lower case
%rlocale’s 12-hour clock time (e.g., 11:11:04 PM)
%R24-hour hour and minute; same as %H:%M
%sseconds since 1970-01-01 00:00:00 UTC
%Ssecond (00..60)
%ta tab
%Ttime; same as %H:%M:%S
%uday of week (1..7); 1 is Monday
%Uweek number of year, with Sunday as first day of week (00..53)
%VISO week number, with Monday as first day of week (01..53)
%wday of week (0..6); 0 is Sunday
%Wweek number of year, with Monday as first day of week (00..53)
%xlocale’s date representation (e.g., 12/31/99)
%Xlocale’s time representation (e.g., 23:13:48)
%ylast two digits of year (00..99)
%Yyear
%z+hhmm numeric timezone (e.g., -0400)
%:z+hh:mm numeric timezone (e.g., -04:00)
%::z+hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::znumeric time zone with : to necessary precision (e.g., -04, +05:30)
%Zalphabetic time zone abbreviation (e.g., EDT)

Also see

Bash-hackers wiki

Shell vars

Learn bash in y minutes

Bash Guide

ShellCheck