‘1324, 5:05 PM. ‘Bash scripting cheatshect
DEVHINTS.10 eit
Bash scripting cheatsheet
‘Adobe Creative Cloud for Teams.
Put creativity to work
ads via Carbon
Introduction Example
This is a quick reference to getting started with Bash scripting. #1 /usr/bin/e,
earn bash fe name="John"’
earn bash in y minutes (learnxinyminutes: Scho “Hello «
Bash Guide (mywik ige.orc
Bash Hackers Wiki (wiki bash 15.016 Variables
String quotes ‘name="Johin’"
echo Sname
7
hname=" John" ne
echo "ili Sname" #=> Hi John
echo ‘Hi Same! #=> Hi Sname
ot
Shell execution
echo "I'm in $(pwd)"
echo “I'm in ‘puid’" # obsolescent
# Same
Conditional e
‘See Command substitution
git commit &
git conmit |
Functions
Conditionals
Intpssdevhints.ofbash ane‘324, 5:05 PM. ‘Bash scripting cheatshect
get_name() {
echo "John"
echo "You are $(get_name)"
See: Functions
Strict mode
set -euo pipefail
IFS=$'\n\t"
See: Unofficial bash strict mode
* Parameter expansions
Basics
name="John"
echo "s{name}"
echo "S{name/J/j}" #=> "john" (substitution)
echo "${name:0:2}" #=> "Jo" (slicing)
echo "S{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"
ength=2
ntpssdevhints.ofbash
af [[ -2 "ss
echo "Stri
elif [[ -n
echo "str
fh
See: Condition
Brace expans
echo {A,B}.j
{A,B}
{A,B)-is
(a..5)
{{1. 3}, {7.0!
See: Brace expa
Substitution
S${fooxsur fix
S${foo#pretix
${FooRxsut fa;
${foo/xsut fi:
${footiprefi:
${foo/#prefi:
ana‘324, 5:05 PM.
echo "${name:0:length}"
See: Parameter expansion
str="/path/to/foo.cpp"
echo "${str%.cpp)" —#
echo "${str&.cpp}.o" #
echo "${str%/*} #
echo "S{stri#i*.)"
echo "S{str##s)" #
echo "S{str#*/}" #
echo "S{str#H*/)" #
echo "S{str/foo/bar}" #
str="Hello world"
echo "${str:6:5)"
echo "${str: -5:5}"
"/path/to/foo.cpp"
{srcHH*/}
> "Jo"
Zpath/to/Foo
Jpath/to/Fo0.0
Jpath/to
cpp (extension)
foo.cpp (basepath)
path/to/foo.cpp
foo.cpp
Ypath/to/bar .cpp
# “world”
# “world”
¥=> "foo. cpp" (basepath)
dir=${srcxSbase)} 4=> "/path/to/" (dirpath)
Length
${HFoo}
Default values
${foo: -val}
${foo:=val}
${foo: val}
${foo:2message}
Omitting the
Intpssdevhints.ofbash
‘Bash scripting cheatshect
${foo/from/ts
${foo//From/:
${foo/xfrom/
${foo/#from/
Comments
# Single Lin
This is a
multi Line
comment
Substrings
${f00:0:3}
${Foo:(-3):3
Manipulation
Le!
str="HELLO W
echo "s{str,
fecha "Sistr,
w
Sfoo, of val if unset (or null)
rs
Set $foo to val if unset (or null)
val if $fo0 is set (and not null)
‘Show error message and exit if $f00 is unset (or null)
removes the (non)nullity checks, e.g. ${foo-val} expands to val if unset otherwise $foo.
a2‘ Loops
Basic for loop
for i in /ete/re.*; do
echo "$i"
done
Ranges
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
myfunc() {
echo "hello $1"
# Same as above (alternate syntax)
function myfunc() {
echo “hello $1"
myfune "John"
Intpssdevhints.ofbash
‘Bash scripting cheatshect
C-like for loo
for ((i = ©
echo "$i"
done
Reading lines
while read -
echo "$Lin
done
foo) shopt -s nul.
set 0 errexit i Used to exit upon error, avoiding cascading errors shopt -s fai
set -o pipefail # Unveils hidden failures shopt 5 noc.
set -o nounset. # Exposes unset variables shopt -s dot:
shopt -s glol
Set GLOBTGNOR
* History
Commands Expansions
history 1s
shopt -s histverify Don't execute expanded result !*
ion
Operations
" Execute last command again
11 :5/// Replace first occurrence of to in most recent command
lices
11 1g/// Replace all occurrences of to in most recelt command
iste Expand only basename from last parameter of most rece! 1:
sh Expand only directory from last parameter of most rece!
11 and 1$ can be replaced with any valid expansion. $
Nin-m
Lins
Intpssdevhints.ofbash sina‘1324, 5:05 PM. ‘Bash scripting cheatshect
Miscellaneous
Numeric calculations
$((a + 200)) # Add 200 to $a
$((SRANDOMK200)) # Random number 0. .199
declare -i count # Declare as type integer
count+=1, # Increment
Inspecting commands
command -V ed
=> "ed is a function/alias/whatever"
Trap errors
trap ‘echo Error at about $LINENO' ERR
or
traperr() {
echo "ERROR: S{BASH_SOURCE[1]} at about ${BASH_LINENO[9]}"
}
set -o errtrace
trap traperr ERR
Source relative
Intpssdevhints.ofbash
1 can be repla
Subshells
(od somedir;
pud # still
Redirection
python hello
python hello
python hello
python hello
nwthan hello
lo
lo
wa
python hello
diff <(s -r
Case/switch
case "Si" in
start | up
vagrant 1
»
echo "Us:
esac
gina‘324, 5:05 PM.
source "${0%/*}/../share/foo.sh"
Transform strings
(Cupper:]
[:lower:]
[:digit:]
Lispace:]
[:alpha:}
[:alnum:]
Example
echo "Welcome To Devhints" | tr '[:lower:]'
WELCOME TO DEVHINTS
Heredoc
cat <