Bash Scripting Cheatsheet
Bash Scripting Cheatsheet
IO
Edit
Introduction Example
NAME="John"
(learnxinyminutes.com)
Bash Guide
String quotes
(mywiki.wooledge.org)
NAME="John"
Functions
git commit || echo "Commit failed"
get_name() {
IFS=$'\n\t'
See: Functions
See: Unofficial bash strict mode
Brace expansion
echo {A,B}.js
{A,B}
{A,B}.js
{1..5}
# Parameter expansions
Basics Substitution
name="John"
${FOO%suffix}
echo ${name}
${FOO//from/to}
length=2
${FOO/%from/to}
${FOO/#from/to}
See: Parameter expansion
STR="/path/to/foo.cpp"
Length
echo ${STR%.cpp} # /path/to/foo
${#FOO}
echo ${STR%/*} # /path/to
Default values
echo ${STR##*/} # foo.cpp (basepath)
${FOO:=val}
echo ${STR/foo/bar} # /path/to/bar.cpp
${FOO:+val}
${FOO:?message}
STR="Hello world"
SRC="/path/to/foo.cpp"
# Loops
Basic for loop C-like for loop
for i in /etc/rc.*; do
for ((i = 0 ; i < 100 ; i++)); do
echo $i
echo $i
done
done
echo $line
···
done
done
# Functions
Defining functions Returning values
myfunc() { myfunc() {
}
echo $myresult
function myfunc() {
result="$(myfunc)"
Arguments
myfunc "John"
$#
$*
$@
$1
$_
# Conditionals
Conditions File conditions
[[ -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 ]]
More conditions
[[ ! EXPR ]] Not
[[ X && Y ]] And
[[ X || Y ]] Or
# Arrays
Defining arrays Working with
echo ${#Frui
Fruits[1]="Banana"
echo ${#Frui
Fruits[2]="Orange"
echo ${#Frui
echo ${Fruit
echo ${!Frui
Operations
for i in "${
unset Fruits[2] # Remove one item
echo $i
Fruits=("${Fruits[@]}") # Duplicate
done
# Dictionaries
Defining Working with dictionaries
declare -A sounds
echo ${sounds[dog]} # Dog's sound
sounds[dog]="bark"
sounds[bird]="tweet"
sounds[wolf]="howl"
# Options
Options Glob options
Set GLOBIGNOR
# History
Commands Expansions
history Show
!$ history
!!:gs/<FROM>/<TO>/
Slices
Replace all occurrences of <FROM> to <TO> in most recent command
!$:t Expand only basename from last parameter of most recent command
!!:n
!$:h Expand only directory from last parameter of most recent command
!^
!$
!! and !$ can be replaced with any valid expansion.
!!:n-m
!!:n-$
!! can be repla
# Miscellaneous
Numeric calculations Subshells
Redirection
Inspecting commands
python hello
python hello
command -V cd
python hello
#=> "cd is a function/alias/whatever"
python hello
python hello
python hello
Trap errors
python hello
diff <(ls -r
trap 'echo Error at about $LINENO' ERR
Case/switch
or
case "$1" in
traperr() {
start | up
echo "ERROR: ${BASH_SOURCE[1]} at about ${BASH_LINENO[0]}"
vagrant
}
;;
set -o errtrace
*)
source "${0%/*}/../share/foo.sh"
printf
printf "This
-s Replaces repeated characters with single occurrence
#=> "This is
-t Truncates
;;
-f | --fla
Heredoc flag=1
;;
cat <<END
esac; shift;
hello world
if [[ "$1" ==
END
Reading inpu
Special variables
echo -n "Pro
read ans
pwd # /home/
Check for command’s result
fi
if grep -q '
echo "You
fi
# Also see
Bash-hackers wiki (bash-hackers.org)
ShellCheck (shellcheck.net)
32 Comments
for this cheatsheet.
Write yours!
Devhints home
cheatsheet
cheatsheet
cheatsheet