Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
22 views

Bash Scripting Cheatsheet

This document provides a cheatsheet for Bash scripting. It covers Bash basics like variables, strings, conditionals, loops and functions. Key topics include parameter expansion, command substitution, brace expansion and strict mode.

Uploaded by

Traxx
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Bash Scripting Cheatsheet

This document provides a cheatsheet for Bash scripting. It covers Bash basics like variables, strings, conditionals, loops and functions. Key topics include parameter expansion, command substitution, brace expansion and strict mode.

Uploaded by

Traxx
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Bash scripting cheatsheet

Introduction Example Variables

This is a quick reference to getting started with#!/usr/bin/env


Bash scripting. bash
NAME="John"


echo $NAME

NAME="John"
echo "$NAME"

Learn bash in y minutes echo "Hello $NAME!"


echo "${NAME}!"

(learnxinyminutes.com)

Bash Guide String quotes Shell execution


(mywiki.wooledge.org)
NAME="John"
echo "I'm in $(pwd)"

echo "Hi $NAME" #=> Hi John


echo "I'm in `pwd`"

Conditional execution echo 'Hi $NAME' #=> Hi $NAME


# Same

git commit && git push


Functions See Command substitution
git commit || echo "Commit failed"

get_name() {
Conditionals
Strict mode echo "John"

if [[ -z "$string" ]]; then

set -euo pipefail


echo "String is empty"

echo "You are $(get_name)"

IFS=$'\n\t'
elif [[ -n "$string" ]]; then

echo "String is not empty"

See: Functions fi

See: Unofficial bash strict mode

Brace expansion See: Conditionals


echo {A,B}.js

{A,B}

{A,B}.js

{1..5}

See: Brace expansion

# Parameter expansions
Basics Substitution Comments

name="John"
${FOO%suffix} # Single line comment

echo ${name}

echo ${name/J/j} #=> ${FOO#prefix}


"john" (substitution)

: '

echo ${name:0:2} #=>


"Jo" (slicing)

${FOO%%suffix} This is a

echo ${name::2} #=>


"Jo" (slicing)

multi line

echo ${name::-1} #=>


"Joh" (slicing)

${FOO##prefix} comment

echo ${name:(-1)} #=> "n" (slicing from right)

'

echo ${name:(-2):1} #=> "h" (slicing from right)

echo ${food:-Cake} #=> $food or "Cake"


${FOO/from/to}
Substrings
${FOO//from/to}
length=2

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


${FOO/%from/to} ${FOO:0:3}

${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!"

echo ${STR%/*} # /path/to

Default values echo ${STR,} #=> "hELLO WORLD!"


echo ${STR,,} #=> "hello world!"
echo ${STR##*.} # cpp (extension)

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


${FOO:-val}
STR="hello world!"

${FOO:=val} echo ${STR^} #=> "Hello world!"


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

echo ${STR^^} #=> "HELLO WORLD!"


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

${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"

echo ${STR: -5:5} # "world"

SRC="/path/to/foo.cpp"

BASE=${SRC##*/} #=> "foo.cpp" (basepath)

DIR=${SRC%$BASE} #=> "/path/to/" (dirpath)

# 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

Reading lines Forever With step size

for i in {5..50..5}; do

cat file.txt | while read line; do


while true; do
echo "Welcome $i"

echo $line
···
done

done
done

# Functions
Defining functions Returning values Raising errors

myfunc() {
myfunc() {
myfunc() {

echo "hello $1"


local myresult='some value'
return 1

}
echo $myresult
}

# Same as above (alternate syntax)


if myfunc; then

function myfunc() {
result="$(myfunc)"
echo "success"

echo "hello $1"


else

}
echo "failure"

Arguments fi

myfunc "John"

$#

$*

$@

$1
$_

Note: $@ and $* must be quoted in order to perform as described.


Otherwise, they do exactly
strings).

See Special parameters.

# 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

[[ -r FILE ]] echo "String is empty"

elif [[ -n "$string" ]]; then

[[ -z STRING ]] [[ -h FILE ]] Empty string


echo "String is not empty"

else

[[ -n STRING ]] [[ -d FILE ]] Not empty string


echo "This never happens"

fi

[[ STRING == STRING ]] [[ -w FILE ]] Equal

[[ STRING != STRING ]] [[ -s FILE ]] # Combinations


Not Equal
if [[ X && Y ]]; then

[[ NUM -eq NUM ]] [[ -f FILE ]] ...


Equal
fi

[[ NUM -ne NUM ]] [[ -x FILE ]] Not equal


[[ NUM -lt NUM ]] [[ FILE1 -nt FILE2 ]] # Equal
Less than
if [[ "$A" == "$B" ]]

[[ NUM -le NUM ]] [[ FILE1 -ot FILE2 ]] Less than or equal

# Regex

[[ NUM -gt NUM ]] [[ FILE1 -ef FILE2 ]] Greater than


if [[ "A" =~ . ]]

[[ NUM -ge NUM ]] Greater than or equal


if (( $a < $b )); then

[[ STRING =~ STRING ]] echo "$a is smaller Regexp


than $b"

fi

(( NUM < NUM )) Numeric conditions

More conditions if [[ -e "file.txt" ]]; then

echo "file exists"

[[ -o noclobber ]] If OPTIONNAME is enabled


fi

[[ ! EXPR ]] Not

[[ X && Y ]] And

[[ X || Y ]] Or

# Arrays
Defining arrays Working with arrays

Fruits=('Apple' 'Banana' 'Orange')


echo ${Fruits[0]} # Element #0

echo ${Fruits[-1]} # Last element

echo ${Fruits[@]} # All elements, space-sepa


Fruits[0]="Apple"

echo ${#Fruits[@]} # Number of elements

Fruits[1]="Banana"

echo ${#Fruits} # String length of the 1st


Fruits[2]="Orange"

echo ${#Fruits[3]} # String length of the Nth


Operations echo ${Fruits[@]:3:2} # Range (from position 3,
echo ${!Fruits[@]} # Keys of all elements, sp

Fruits=("${Fruits[@]}" "Watermelon") # Push

Iteration
Fruits+=('Watermelon') # Also Push

Fruits=( ${Fruits[@]/Ap*/} ) # Remove by regex match

unset Fruits[2] # Remove one item


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

Fruits=("${Fruits[@]}") # Duplicate
echo $i

Fruits=("${Fruits[@]}" "${Veggies[@]}") # Concatenate


done

lines=(`cat "logfile"`) # Read from file

# Dictionaries
Defining Working with dictionaries Iteration

Iterate over values


declare -A sounds
echo ${sounds[dog]} # Dog's sound

echo ${sounds[@]} # All values

for val in "${sounds[@]}"; do

echo ${!sounds[@]} # All keys


sounds[dog]="bark"
echo $val

echo ${#sounds[@]} # Number of elements

sounds[cow]="moo"
done

unset sounds[dog] # Delete dog

sounds[bird]="tweet"

sounds[wolf]="howl"
Iterate over keys
for key in "${!sounds[@]}"; do

Declares sound as a Dictionary object (aka associative array). echo $key

done

# Options
Options Glob options

set -o noclobber # Avoid overlay files (echo "hi" > foo)


shopt -s nullglob # Non-matching globs are removed
set -o errexit # Used to exit upon error, avoiding cascading shopt
errors

-s failglob # Non-matching globs throw errors


set -o pipefail # Unveils hidden failures
shopt -s nocaseglob # Case insensitive globs

set -o nounset # Exposes unset variables


shopt -s dotglob # Wildcards match dotfiles ("*.sh
shopt -s globstar # Allow ** for recursive matches

Set GLOBIGNORE as a colon-separated list of patterns to be remove

# History
Commands Expansions

history !$ Show history

shopt -s histverify !* Don’t execute expanded result immediately


Operations !-n

!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

!!:n Expand only


!$: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.
!!:n-m

!!:n-$

!! can be replaced with any valid expansion i.e. !cat, !-2, !42, etc

# Miscellaneous
Numeric calculations Subshells

$((a + 200)) # Add 200 to $a


(cd somedir; echo "I'm now in $PWD")

pwd # still in first directory

$(($RANDOM%200)) # Random number 0..199

Redirection
Inspecting commands python hello.py > output.txt # stdout to (file)

python hello.py >> output.txt # stdout to (file), app


command -V cd
python hello.py 2> error.log # stderr to (file)

#=> "cd is a function/alias/whatever"


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


Trap errors

python hello.py < foo.txt # feed foo.txt to stdin


trap 'echo Error at about $LINENO' ERR
diff <(ls -r) <(ls) # Compare two stdout wi

or
Case/switch
traperr() {

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


case "$1" in

}
start | up)

vagrant up

set -o errtrace
;;

trap traperr ERR

*)

Source relative echo "Usage: $0 {start|stop|ssh}"

;;

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

printf "1 + 1 = %d" 2

Getting options #=> "1 + 1 = 2"

printf "This is how you print a float: %f" 2

while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in


#=> "This is how you print a float: 2.000000"

-V | --version )

echo $version Heredoc


exit

;;

-s | --string ) cat <<END

shift; string=$1
hello world

;;
END

-f | --flag )

flag=1

;;

Reading input
esac; shift; done

if [[ "$1" == '--' ]]; then shift; fi


echo -n "Proceed? [y/n]: "

Special variables read ans

echo $ans

$? Exit status of last task


read -n 1 ans # Just one character

$! PID of last background task

Go to previous directory
$$ PID of shell

$0 pwd # /home/user/foo
Filename of the shell script
cd bar/

$_ Last argument of the previous command


pwd # /home/user/foo/bar

cd -

See Special parameters. pwd # /home/user/foo

Grep check Check for command’s result

if grep -q 'foo' ~/.bash_history; then


if ping -c 1 google.com; then

echo "You appear to have typed 'foo' in the past"


echo "It appears you have a working internet connect
fi
fi

You might also like