Bash Scripting Cheatsheet
Bash Scripting Cheatsheet
echo $NAME
NAME="John"
echo "$NAME"
(learnxinyminutes.com)
get_name() {
Conditionals
Strict mode echo "John"
IFS=$'\n\t'
elif [[ -n "$string" ]]; then
See: Functions fi
{A,B}
{A,B}.js
{1..5}
# Parameter expansions
Basics Substitution Comments
name="John"
${FOO%suffix} # Single line comment
echo ${name}
: '
${FOO%%suffix} This is a
multi line
${FOO##prefix} comment
'
${FOO/#from/to} ${FOO:(-3):3}
See: Parameter expansion
STR="/path/to/foo.cpp"
Length Manipulation
echo ${STR%.cpp} # /path/to/foo
echo ${STR%.cpp}.o # /path/to/foo.o
${#FOO}
STR="HELLO WORLD!"
${FOO:+val}
echo ${STR/foo/bar} # /path/to/bar.cpp
${FOO:?message} Show
STR="Hello world"
Omitting the : removes the (non)nullity checks, e.g. ${FOO-val} expands to val if unset othe
echo ${STR:6:5} # "world"
SRC="/path/to/foo.cpp"
# Loops
Basic for loop C-like for loop Ranges
for i in /etc/rc.*; do
for ((i = 0 ; i < 100 ; i++)); do
for i in {1..5}; do
echo $i
echo $i
echo "Welcome $i"
done
done
done
for i in {5..50..5}; do
echo $line
···
done
done
done
# Functions
Defining functions Returning values Raising errors
myfunc() {
myfunc() {
myfunc() {
}
echo $myresult
}
function myfunc() {
result="$(myfunc)"
echo "success"
}
echo "failure"
Arguments fi
myfunc "John"
$#
$*
$@
$1
$_
# Conditionals
Conditions File conditions Example
[[ -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
# String
utils, such as grep(1) or ping(1)) can be used as condition, see examples. if [[ -z "$string" ]]; then
else
fi
# Regex
fi
[[ ! EXPR ]] Not
[[ X && Y ]] And
[[ X || Y ]] Or
# Arrays
Defining arrays Working with arrays
Fruits[1]="Banana"
Iteration
Fruits+=('Watermelon') # Also Push
Fruits=("${Fruits[@]}") # Duplicate
echo $i
# Dictionaries
Defining Working with dictionaries Iteration
sounds[cow]="moo"
done
sounds[bird]="tweet"
sounds[wolf]="howl"
Iterate over keys
for key in "${!sounds[@]}"; do
done
# Options
Options Glob options
# History
Commands Expansions
!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
!^
!$:h Expand only directory from last parameter of most recent command
!$
!! and !$ can be replaced with any valid expansion.
!!:n-m
!!:n-$
!! can be replaced with any valid expansion i.e. !cat, !-2, !42, etc
# Miscellaneous
Numeric calculations Subshells
Redirection
Inspecting commands python hello.py > output.txt # stdout to (file)
or
Case/switch
traperr() {
}
start | up)
vagrant up
set -o errtrace
;;
*)
;;
esac
source "${0%/*}/../share/foo.sh"
printf
Directory of script
printf "Hello %s, I'm %s" Sven Olga
DIR="${0%/*}"
#=> "Hello Sven, I'm Olga
-V | --version )
;;
shift; string=$1
hello world
;;
END
-f | --flag )
flag=1
;;
Reading input
esac; shift; done
echo $ans
Go to previous directory
$$ PID of shell
$0 pwd # /home/user/foo
Filename of the shell script
cd bar/
cd -