0% found this document useful (0 votes)
152 views12 pages

Bash Scripting Cheatsheet

This document provides an overview of key concepts for getting started with Bash scripting. It covers topics like variables, conditionals, loops, functions, parameter expansions and more with examples for each. Links are also provided to external resources for further Bash scripting reference and tutorials.

Uploaded by

aarickkhanna1234
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
152 views12 pages

Bash Scripting Cheatsheet

This document provides an overview of key concepts for getting started with Bash scripting. It covers topics like variables, conditionals, loops, functions, parameter expansions and more with examples for each. Links are also provided to external resources for further Bash scripting reference and tutorials.

Uploaded by

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

Bash scripting

cheatsheet

Introduction Variables

name="John"
This is a quick reference to getting started
echo $name # see below
with Bash scripting. echo "$name"
echo "${name}!"
Learn bash in y minutes
(learnxinyminutes.com)
Generally quote your variables unless they contain
wildcards to expand or command fragments.
Bash Guide
(mywiki.wooledge.org)
wildcard="*.txt"
Bash Hackers Wiki options="iv"
cp -$options $wildcard /tmp
(wiki.bash-hackers.org)

Example String quotes

#!/usr/bin/env bash name="John"


echo "Hi $name" #=> Hi John
name="John" echo 'Hi $name' #=> Hi $name
echo "Hello $name!"

Shell execution

echo "I'm in $(pwd)"


echo "I'm in `pwd`" # obsolescent
# Same

See Command substitution


Conditional execution Strict mode

git commit && git push set -euo pipefail


git commit || echo "Commit failed" IFS=$'\n\t'

See: Unofficial bash strict mode

Functions

get_name() {
Brace expansion
echo "John"
}
echo {A,B}.js
echo "You are $(get_name)"
{A,B} Same as A B

See: Functions
{A,B}.js Same as A.js B.js

{1..5} Same as 1 2 3 4 5

{{1..3},{7..9}} Same as 1 2 3 7 8 9
Conditionals

See: Brace expansion


if [[ -z "$string" ]]; then
echo "String is empty"
elif [[ -n "$string" ]]; then
echo "String is not empty"
fi

See: Conditionals

Parameter expansions
Basics Indirection

name="John" name=joe
echo "${name}" pointer=name
echo "${name/J/j}" #=> "john" (substitution) echo ${!pointer}
echo "${name:0:2}" #=> "Jo" (slicing) joe
echo "${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" Substitution

${foo%suffix} Remove suffix


length=2
echo "${name:0:length}" #=> "Jo"
${foo#prefix} Remove prefix

See: Parameter expansion ${foo%%suffix} Remove long suffix

${foo/%suffix} Remove long suffix


str="/path/to/foo.cpp"
echo "${str%.cpp}" # /path/to/foo
${foo##prefix} Remove long prefix
echo "${str%.cpp}.o" # /path/to/foo.o
echo "${str%/*}" # /path/to
${foo/#prefix} Remove long prefix
echo "${str##*.}" # cpp (extension)
echo "${str##*/}" # foo.cpp (basepath)
${foo/from/to} Replace first match

echo "${str#*/}" # path/to/foo.cpp ${foo//from/to} Replace all


echo "${str##*/}" # foo.cpp
${foo/%from/to} Replace suffix
echo "${str/foo/bar}" # /path/to/bar.cpp
${foo/#from/to} Replace prefix

str="Hello world"
echo "${str:6:5}" # "world"
echo "${str: -5:5}" # "world"
Comments
src="/path/to/foo.cpp"
base=${src##*/} #=> "foo.cpp" (basepath) # Single line comment
dir=${src%$base} #=> "/path/to/" (dirpath)
: '
This is a
multi line
comment
Prefix name expansion '

prefix_a=one
prefix_b=two
echo ${!prefix_*} # all variables names starting with `prefix_
prefix_a prefix_b
Substrings Default values

${foo:0:3} Substring (position, length) ${foo:-val} $foo, or val if unset (or null)

${foo:(-3):3} Substring from the right ${foo:=val} Set $foo to val if unset (or
null)

${foo:+val} val if $foo is set (and not

Length null)

${foo:?message} Show error message and exit


${#foo} Length of $foo
if $foo is unset (or null)

Omitting the : removes the (non)nullity checks, e.g.

Manipulation ${foo-val} expands to val if unset otherwise $foo.

str="HELLO WORLD!"
echo "${str,}" #=> "hELLO WORLD!" (lowercase 1st letter)
echo "${str,,}" #=> "hello world!" (all lowercase)

str="hello world!"
echo "${str^}" #=> "Hello world!" (uppercase 1st letter)
echo "${str^^}" #=> "HELLO WORLD!" (all uppercase)

Loops

Basic for loop Ranges

for i in /etc/rc.*; do for i in {1..5}; do


echo "$i" echo "Welcome $i"
done done

With step size

for i in {5..50..5}; do
C-like for loop echo "Welcome $i"
done

for ((i = 0 ; i < 100 ; i++)); do


echo "$i"
done
Reading lines Forever

while read -r line; do while true; do


echo "$line" ···
done <file.txt done

Functions

Defining functions Raising errors

myfunc() { myfunc() {
echo "hello $1" return 1
} }

# Same as above (alternate syntax) if myfunc; then


function myfunc { echo "success"
echo "hello $1" else
} echo "failure"
fi

myfunc "John"

Arguments
Returning values
$# Number of arguments

myfunc() {
$* All positional arguments (as a single word)
local myresult='some value'
echo "$myresult"
$@ All positional arguments (as separate strings)
}

$1 First argument
result=$(myfunc)
$_ Last argument of the previous command

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


as described. Otherwise, they do exactly the same
thing (arguments as separate strings).

See Special parameters.


Conditionals

Conditions File conditions

[[ -e FILE ]] Exists
Note that [[ is actually a command/program that
returns either 0 (true) or 1 (false). Any program that
[[ -r FILE ]] Readable
obeys the same logic (like all base utils, such as
grep(1) or ping(1)) can be used as condition, see [[ -h FILE ]] Symlink
examples.
[[ -d FILE ]] Directory
[[ -z STRING ]] Empty string
[[ -w FILE ]] Writable
[[ -n STRING ]] Not empty string
[[ -s FILE ]] Size is > 0 bytes
[[ STRING == STRING ]] Equal
[[ -f FILE ]] File
[[ STRING != STRING ]] Not Equal
[[ -x FILE ]] Executable
[[ NUM -eq NUM ]] Equal
[[ FILE1 -nt FILE2 ]] 1 is more recent than
[[ NUM -ne NUM ]] Not equal 2

[[ NUM -lt NUM ]] Less than [[ FILE1 -ot FILE2 ]] 2 is more recent than
1
[[ NUM -le NUM ]] Less than or equal
[[ FILE1 -ef FILE2 ]] Same files
[[ NUM -gt NUM ]] Greater than

[[ NUM -ge NUM ]] Greater than or


equal

[[ STRING =~ STRING ]] Regexp

(( NUM < NUM )) Numeric conditions

More conditions

[[ -o noclobber ]] If OPTIONNAME is
enabled

[[ ! EXPR ]] Not

[[ X && Y ]] And

[[ X || Y ]] Or
Example

# String
if [[ -z "$string" ]]; then
echo "String is empty"
elif [[ -n "$string" ]]; then
echo "String is not empty"
else
echo "This never happens"
fi

# Combinations
if [[ X && Y ]]; then
...
fi

# Equal
if [[ "$A" == "$B" ]]

# Regex
if [[ "A" =~ . ]]

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


echo "$a is smaller than $b"
fi

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


echo "file exists"
fi

Arrays

Defining arrays Working with arrays

Fruits=('Apple' 'Banana' 'Orange') echo "${Fruits[0]}" # Element #0


echo "${Fruits[-1]}" # Last element
echo "${Fruits[@]}" # All elements, spa
Fruits[0]="Apple"
echo "${#Fruits[@]}" # Number of element
Fruits[1]="Banana"
echo "${#Fruits}" # String length of
Fruits[2]="Orange"
echo "${#Fruits[3]}" # String length of
echo "${Fruits[@]:3:2}" # Range (from posit
echo "${!Fruits[@]}" # Keys of all eleme
Operations Iteration

Fruits=("${Fruits[@]}" "Watermelon") # Push for i in "${arrayName[@]}"; do


Fruits+=('Watermelon') # Also Push echo "$i"
Fruits=( "${Fruits[@]/Ap*/}" ) # Remove by
done
regex match
unset Fruits[2] # Remove one item
Fruits=("${Fruits[@]}") # Duplicate
Fruits=("${Fruits[@]}" "${Veggies[@]}") # Concatenate
lines=(`cat "logfile"`) # Read from file

Dictionaries

Defining Working with dictionaries

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


echo "${sounds[@]}" # All values
echo "${!sounds[@]}" # All keys
sounds[dog]="bark"
echo "${#sounds[@]}" # Number of elements
sounds[cow]="moo"
unset sounds[dog] # Delete dog
sounds[bird]="tweet"
sounds[wolf]="howl"

Declares sound as a Dictionary object (aka


Iteration
associative array).
Iterate over values

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


echo "$val"
done

Iterate over keys

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


echo "$key"
done

Options
Options Glob options

set -o noclobber # Avoid overlay files (echo "hi"shopt


> foo)
-s nullglob # Non-matching globs are rem
set -o errexit # Used to exit upon error, avoiding
shoptcascading
-s failglob # Non-matching globs throw e
set -o pipefail # Unveils hidden failures shopt -s nocaseglob # Case insensitive globs
set -o nounset # Exposes unset variables shopt -s dotglob # Wildcards match dotfiles (
shopt -s globstar # Allow ** for recursive mat

Set GLOBIGNORE as a colon-separated list of patterns


to be removed from glob matches.

History

Commands Operations

history Show history !! Execute last command


again
shopt -s histverify Don’t execute
expanded result !!:s/<FROM>/<TO>/ Replace first occurrence
immediately of <FROM> to <TO> in
most recent command

!!:gs/<FROM>/<TO>/ Replace all occurrences

Expansions of <FROM> to <TO> in


most recent command
!$ Expand last parameter of most
!$:t Expand only basename
recent command
from last parameter of
!* Expand all parameters of most most recent command
recent command
!$:h Expand only directory
!-n Expand nth most recent command from last parameter of
most recent command
!n Expand nth command in history
!! and !$ can be replaced with any valid expansion.
!<command> Expand most recent invocation of
command <command>
Slices

!!:n Expand only nth token from most


recent command (command is 0; first
argument is 1)

!^ Expand first argument from most recent


command

!$ Expand last token from most recent


command

!!:n-m Expand range of tokens from most


recent command

!!:n-$ Expand nth token to last from most


recent command

!! can be replaced with any valid expansion i.e. !


cat, !-2, !42, etc.

Miscellaneous

Numeric calculations Redirection

$((a + 200)) # Add 200 to $a python hello.py > output.txt # stdout


python hello.py >> output.txt # stdout
python hello.py 2> error.log # stderr
$(($RANDOM%200)) # Random number 0..199
python hello.py 2>&1 # stderr
python hello.py 2>/dev/null # stderr
declare -i count # Declare as type integer python hello.py >output.txt 2>&1 # stdout
count+=1 # Increment python hello.py &>/dev/null # stdout
echo "$0: warning: too many users" >&2 # print d

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


Subshells diff <(ls -r) <(ls) # Compare two stdo

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


pwd # still in first directory
Inspecting commands printf

command -V cd printf "Hello %s, I'm %s" Sven Olga


#=> "cd is a function/alias/whatever" #=> "Hello Sven, I'm Olga

printf "1 + 1 = %d" 2


#=> "1 + 1 = 2"

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


#=> "This is how you print a float: 2.000000"
trap 'echo Error at about $LINENO' ERR
printf '%s\n' '#!/bin/bash' 'echo hello' >file
# format string is applied to each group of argum
or printf '%i+%i=%i\n' 1 2 3 4 5 9

traperr() {
echo "ERROR: ${BASH_SOURCE[1]} at about ${BASH_LINENO
}
Transform strings
set -o errtrace
trap traperr ERR -c Operations apply to characters not
in the given set

-d Delete characters

Case/switch -s Replaces repeated characters with


single occurrence
case "$1" in
start | up) -t Truncates
vagrant up
;; [:upper:] All upper case letters

*) [:lower:] All lower case letters


echo "Usage: $0 {start|stop|ssh}"
;; [:digit:] All digits
esac
[:space:] All whitespace

[:alpha:] All letters

Source relative [:alnum:] All letters and digits

Example
source "${0%/*}/../share/foo.sh"
echo "Welcome To Devhints" | tr '[:lower:]' '[:up
WELCOME TO DEVHINTS
Directory of script Special variables

dir=${0%/*} $? Exit status of last task

$! PID of last background task

$$ PID of shell
Getting options
$0 Filename of the shell script
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case
-V | --version ) $_ Last argument of the
echo "$version" previous command
exit
;; ${PIPESTATUS[n]} return value of piped
-s | --string ) commands (array)
shift; string=$1
;; See Special parameters.
-f | --flag )
flag=1
;;
esac; shift; done
if [[ "$1" == '--' ]]; then shift; fi Go to previous directory

pwd # /home/user/foo
cd bar/
Heredoc pwd # /home/user/foo/bar
cd -
pwd # /home/user/foo
cat <<END
hello world
END

Check for command’s result

Reading input if ping -c 1 google.com; then


echo "It appears you have a working internet co
fi
echo -n "Proceed? [y/n]: "
read -r ans
echo "$ans"

Grep check
The -r option disables a peculiar legacy behavior
with backslashes.
if grep -q 'foo' ~/.bash_history; then
echo "You appear to have typed 'foo' in the pas
read -n 1 ans # Just one character fi

You might also like