Bash Script Cheat Sheets
Bash Script Cheat Sheets
DEVHINTS.IO Edit
Example Variables St
Sh
Conditional execution Functions
echo {A,B}.js
See: Conditionals
{A,B}
{A,B}.js
{1..5}
https://devhints.io/bash 1/9
8/31/2019 Bash scripting cheatsheet
# Parameter expansions
Basics Substitution Co
name="John" ${FOO%suffix}
echo ${name}
echo ${name/J/j} #=> "john" (substitution) ${FOO#prefix}
echo ${name:0:2} #=> "Jo" (slicing)
echo ${name::2} #=> "Jo" (slicing) ${FOO%%suffix}
echo ${name::-1} #=> "Joh" (slicing)
echo ${name:(-1)} #=> ${FOO##prefix}
"n" (slicing from right)
echo ${name:(-2):1} #=> "h" (slicing from right)
echo ${food:-Cake} #=> $food or "Cake" ${FOO/from/to}
${FOO//from/to}
Su
length=2
echo ${name:0:length} #=> "Jo"
${FOO/%from/to}
${FOO/#from/to}
See: Parameter expansion
Length M
STR="/path/to/foo.cpp"
echo ${STR%.cpp} # /path/to/foo
echo ${STR%.cpp}.o # /path/to/foo.o ${#FOO}
STR="Hello world"
${FOO:?message}
echo ${STR:6:5} # "world"
echo ${STR:-5:5} # "world"
The : is optional (eg, ${FOO=word} works)
SRC="/path/to/foo.cpp"
BASE=${SRC##*/} #=> "foo.cpp" (basepath)
DIR=${SRC%$BASE} #=> "/path/to/" (dirpath)
https://devhints.io/bash 2/9
8/31/2019 Bash scripting cheatsheet
# Loops
Basic for loop C-like for loop Ra
# Functions
Defining functions Returning values Ra
myfunc() { myfunc() {
echo "hello $1" local myresult='some value'
} echo $myresult
}
$*
$@
$1
https://devhints.io/bash 3/9
8/31/2019 Bash scripting cheatsheet
# Conditionals
Conditions File conditions Ex
[[ -e FILE ]]
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.
[[ -r FILE ]]
[[ ! EXPR ]] Not
[[ X ]] && [[ Y ]] And
[[ X ]] || [[ Y ]] Or
https://devhints.io/bash 4/9
8/31/2019 Bash scripting cheatsheet
# Arrays
Defining arrays Working with arrays
Operations Iteration
# Dictionaries
Defining Working with dictionaries Ite
https://devhints.io/bash 5/9
8/31/2019 Bash scripting cheatsheet
# Options
Options Glob options
set -o noclobber # Avoid overlay files (echo "hi" > foo) set -o nullglob # No
set -o errexit # Used to exit upon error, avoiding cascading errorsset -o failglob # No
set -o pipefail # Unveils hidden failures set -o nocaseglob # Cas
set -o nounset # Exposes unset variables set -o globdots # Wil
set -o globstar # All
# History
Commands Expansions
!-n
Operations
!n
!! Execute last command again
!<command>
!!:s/<FROM>/<TO>/ Replace first occurrence of <FROM> to <TO> in most recent command
Slices
!!:gs/<FROM>/<TO>/ Replace all occurrences of <FROM> to <TO> in most recent command
!$
!! and !$ can be replaced with any valid expansion.
!!:n-m
!!:n-$
https://devhints.io/bash 6/9
8/31/2019 Bash scripting cheatsheet
# Miscellaneous
Numeric calculations Subshells
Redirection
Inspecting commands
python hello.py > outpu
python hello.py >> outp
command -V cd
python hello.py 2> erro
#=> "cd is a function/alias/whatever"
python hello.py 2>&1
python hello.py 2>/dev/
python hello.py &>/dev/
Trap errors
or
Case/switch
traperr() {
case "$1" in
echo "ERROR: ${BASH_SOURCE[1]} at about ${BASH_LINENO[0]}"
start | up)
}
vagrant up
;;
set -o errtrace
trap traperr ERR
*)
echo "Usage: $0 {sta
;;
Source relative esac
source "${0%/*}/../share/foo.sh"
printf
https://devhints.io/bash 7/9
8/31/2019 Bash scripting cheatsheet
Special variables
Go to previous directory
$?
pwd # /home/user/foo $!
cd bar/
pwd # /home/user/foo/bar $$
cd -
pwd # /home/user/foo
See Special parameters.
# Also see
Bash-hackers wiki (bash-hackers.org)
ShellCheck (shellcheck.net)
https://devhints.io/bash 8/9
8/31/2019 Bash scripting cheatsheet
Devhints home
https://devhints.io/bash 9/9