0% found this document useful (0 votes)
118 views20 pages

Bash Scripting Cheat Sheet Guide

This cheat sheet provides concise summaries of key aspects of Bash scripting including: 1) Variables, strings, command substitution, conditionals, functions and parameter expansion in 3 sentences or less. 2) Loops, arrays, dictionaries, options, and history commands are defined with examples given in 1 sentence each. 3) File and string conditions are listed along with examples for testing various properties.

Uploaded by

only. naila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views20 pages

Bash Scripting Cheat Sheet Guide

This cheat sheet provides concise summaries of key aspects of Bash scripting including: 1) Variables, strings, command substitution, conditionals, functions and parameter expansion in 3 sentences or less. 2) Loops, arrays, dictionaries, options, and history commands are defined with examples given in 1 sentence each. 3) File and string conditions are listed along with examples for testing various properties.

Uploaded by

only. naila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

BS-PLF Cheat sheet

Example
#!/usr/bin/env bash

NAME="John"
echo "Hello $NAME!"

Variables
NAME="John"
echo $NAME
echo "$NAME"
echo "${NAME}!"

String quotes
NAME="John"
echo "Hi $NAME" #=> Hi John
echo 'Hi $NAME' #=> Hi $NAME

Shell execution
echo "I'm in $(pwd)"
echo "I'm in `pwd`"
# Same
See Command substitution

Conditional execution
git commit && git push
git commit || echo "Commit failed"

Functions
get_name() {
echo "John"
}

echo "You are $(get_name)"


See: Functions

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

Strict mode
set -euo pipefail
IFS=$'\n\t'
See: Unofficial bash strict mode
Brace expansion
echo {A,B}.js
{A,B} Same as A B

{A,B}.js Same as A.js B.js

{1..5} Same as 1 2 3 4 5
See: Brace expansion

#Parameter expansions
Basics
name="John"
echo ${name}
echo ${name/J/j} #=> "john" (substitution)
echo ${name:0:2} #=> "Jo" (slicing)
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"
length=2
echo ${name:0:length} #=> "Jo"
See: Parameter expansion
STR="/path/to/foo.cpp"
echo ${STR%.cpp} # /path/to/foo
echo ${STR%.cpp}.o # /path/to/foo.o
echo ${STR%/*} # /path/to

echo ${STR##*.} # cpp (extension)


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

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


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

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


STR="Hello world"
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)

Substitution

${FOO%suffix} Remove suffix

${FOO#prefix} Remove prefix

${FOO%%suffix} Remove long suffix

${FOO##prefix} Remove long prefix

${FOO/from/to} Replace first match


${FOO//from/to} Replace all

${FOO/%from/to} Replace suffix

${FOO/#from/to} Replace prefix

Comments
# Single line comment
: '
This is a
multi line
comment
'

Substrings

${FOO:0:3} Substring (position, length)

${FOO:(-3):3} Substring from the right

Length

${#FOO} Length of $FOO

Manipulation
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)

Default values

${FOO:-val} $FOO, or val if unset (or null)

${FOO:=val} Set $FOO to val if unset (or


null)

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


null)

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


if $FOO is unset (or null)
Omitting the : removes the (non)nullity checks, e.g. ${FOO-val} expands to val if unset otherwise $FOO.

#Loops
Basic for loop
for i in /etc/rc.*; do
echo $i
done

C-like for loop


for ((i = 0 ; i < 100 ; i++)); 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

Reading lines
cat file.txt | while read line; do
echo $line
done

Forever
while true; do
···
done

#Functions
Defining functions
myfunc() {
echo "hello $1"
}
# Same as above (alternate syntax)
function myfunc() {
echo "hello $1"
}
myfunc "John"

Returning values
myfunc() {
local myresult='some value'
echo $myresult
}
result="$(myfunc)"

Raising errors
myfunc() {
return 1
}
if myfunc; then
echo "success"
else
echo "failure"
fi

Arguments

$# Number of arguments

$* All arguments

$@ All arguments, starting from first

$1 First argument

$_ Last argument of the previous command


See Special parameters.

#Conditionals
Conditions
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.
[[ -z STRING ]] Empty string

[[ -n STRING ]] Not empty string

[[ STRING == STRING ]] Equal

[[ STRING != STRING ]] Not Equal

[[ NUM -eq NUM ]] Equal

[[ NUM -ne NUM ]] Not equal

[[ NUM -lt NUM ]] Less than

[[ NUM -le NUM ]] Less than or equal

[[ 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

File conditions

[[ -e FILE ]] Exists

[[ -r FILE ]] Readable

[[ -h FILE ]] Symlink

[[ -d FILE ]] Directory

[[ -w FILE ]] Writable

[[ -s FILE ]] Size is > 0 bytes

[[ -f FILE ]] File

[[ -x FILE ]] Executable

[[ FILE1 -nt FILE2 ]] 1 is more recent than 2

[[ FILE1 -ot FILE2 ]] 2 is more recent than 1

[[ FILE1 -ef FILE2 ]] Same files

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
Fruits=('Apple' 'Banana' 'Orange')
Fruits[0]="Apple"
Fruits[1]="Banana"
Fruits[2]="Orange"

Working with arrays


echo ${Fruits[0]} # Element #0
echo ${Fruits[-1]} # Last element
echo ${Fruits[@]} # All elements, space-separated
echo ${#Fruits[@]} # Number of elements
echo ${#Fruits} # String length of the 1st element
echo ${#Fruits[3]} # String length of the Nth element
echo ${Fruits[@]:3:2} # Range (from position 3, length 2)
echo ${!Fruits[@]} # Keys of all elements, space-separated

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

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

#Dictionaries
Defining
declare -A sounds
sounds[dog]="bark"
sounds[cow]="moo"
sounds[bird]="tweet"
sounds[wolf]="howl"
Declares sound as a Dictionary object (aka associative array).

Working with dictionaries


echo ${sounds[dog]} # Dog's sound
echo ${sounds[@]} # All values
echo ${!sounds[@]} # All keys
echo ${#sounds[@]} # Number of elements
unset sounds[dog] # Delete dog

Iteration
Iterate over values
for val in "${sounds[@]}"; do
echo $val
done
Iterate over keys
for key in "${!sounds[@]}"; do
echo $key
done
#Options
Options
set -o noclobber # Avoid overlay files (echo "hi" > foo)
set -o errexit # Used to exit upon error, avoiding cascading errors
set -o pipefail # Unveils hidden failures
set -o nounset # Exposes unset variables

Glob options
shopt -s nullglob # Non-matching globs are removed ('*.foo' => '')
shopt -s failglob # Non-matching globs throw errors
shopt -s nocaseglob # Case insensitive globs
shopt -s dotglob # Wildcards match dotfiles ("*.sh" => ".foo.sh")
shopt -s globstar # Allow ** for recursive matches ('lib/**/*.rb' => 'lib/a/b/c.rb')
Set GLOBIGNORE as a colon-separated list of patterns to be removed from glob matches.

#History
Commands

history Show history

shopt -s histverify Don’t execute expanded result immediately

Expansions

!$ Expand last parameter of most recent command

!* Expand all parameters of most recent command

!-n Expand nth most recent command

!n Expand nth command in history

!<command> Expand most recent invocation of command <command>

Operations

!! Execute last command again

!!:s/<FROM>/<TO>/ Replace first occurrence of <FROM> to <TO> in most recent


command

!!:gs/<FROM>/<TO>/ Replace all occurrences of <FROM> to <TO> in most recent


command

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

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
$((a + 200)) # Add 200 to $a
$(($RANDOM%200)) # Random number 0..199

Subshells
(cd somedir; echo "I'm now in $PWD")
pwd # still in first directory

Redirection
python hello.py > output.txt # stdout to (file)
python hello.py >> output.txt # stdout to (file), append
python hello.py 2> error.log # stderr to (file)
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 (null)
python hello.py < foo.txt # feed foo.txt to stdin for python

Inspecting commands
command -V cd
#=> "cd is a function/alias/whatever"

Trap errors
trap 'echo Error at about $LINENO' ERR
or
traperr() {
echo "ERROR: ${BASH_SOURCE[1]} at about ${BASH_LINENO[0]}"
}

set -o errtrace
trap traperr ERR
Case/switch
case "$1" in
start | up)
vagrant up
;;

*)
echo "Usage: $0 {start|stop|ssh}"
;;
esac

Source relative
source "${0%/*}/../share/foo.sh"

printf
printf "Hello %s, I'm %s" Sven Olga
#=> "Hello Sven, I'm Olga

printf "1 + 1 = %d" 2


#=> "1 + 1 = 2"

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


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

Directory of script
DIR="${0%/*}"

Getting options
while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do case $1 in
-V | --version )
echo $version
exit
;;
-s | --string )
shift; string=$1
;;
-f | --flag )
flag=1
;;
esac; shift; done
if [[ "$1" == '--' ]]; then shift; fi

Heredoc
cat <<END
hello world
END

Reading input
echo -n "Proceed? [y/n]: "
read ans
echo $ans
read -n 1 ans # Just one character

Special variables
$? Exit status of last task

$! PID of last background task

$$ PID of shell

$0 Filename of the shell script


See Special parameters.

Go to previous directory
pwd # /home/user/foo
cd bar/
pwd # /home/user/foo/bar
cd -
pwd # /home/user/foo

Check for command’s result


if ping -c 1 google.com; then
echo "It appears you have a working internet connection"
fi

Grep check
if grep -q 'foo' ~/.bash_history; then
echo "You appear to have typed 'foo' in the past"
fi

option description

ls -a list all files including hidden file starting with '.'

ls --color colored list [=always/never/auto]

ls -d list directories - with ' */'

ls -F add one char of */=>@| to enteries

ls -i list file's inode index number

ls -l list with long format - show permissions

ls -la list long format including hidden files

ls -lh list long format with readable file size

ls -ls list with long format with file size

ls -r list in reverse order

ls -R list recursively directory tree

ls -s list file size


ls -S sort by file size

ls -t sort by time & date

ls -X sort by extension name

ls command examples
You can press the tab button to auto complete the file or folder names.
List directory Documents/Books with relative path:
$ ls Documents/Books

List directory /home/user/Documents/Books with absolute path.


$ ls /home/user/Documents/Books

List root directory:


$ ls /

List parent directory:


$ ls ..

List user's home directory (e.g: /home/user):


$ ls ~

List with long format:


$ ls -l

Show hidden files:


$ ls -a

List with long format and show hidden files:


$ ls -la

Sort by date/time:
$ ls -t

Sort by file size:


$ ls -S

List all subdirectories:


$ ls *

Recursive directory tree list:


$ ls -R

List only text files with wildcard:


$ ls *.txt

ls redirection to output file:


$ ls > out.txt

List directories only:


$ ls -d */

List files and directories with full path:


$ ls -d $PWD/*

BS – PLF – Reference

Linux Scripting
• var=wert
• ${var}
• “blabla” – NICHT ‘blabla’
• echo “\${var}” -> “${var}” \ - Zeichen mit besonderer Wirkung werden NICHT
ausgewertet
• $(befehl) um Ausgabe des Befehls zu nutzen
• Bash NICHT sh
o bash /Pfad/dateiname.sh
• Manual pages (man) verwenden
• Erste Zeile – „#!/bin/bash“
• $0 = dateiname, $1 = Erster Paramter…
• $@ = alle Parameter in Art Array
• $* = alle Parameter in einem String
• $# = Anzahl an Parametern
• $? = Exit-Status des letzten verwendeten Befehls
• false = nop
• install package:
o sudo apt-get install package

• echo ${var: -a} wenn var keinen Wert besitzt -> Default-Wert = „a“
• echo ${var:=a} wenn var keinen Wert besitzt -> Default-Wert = „a“ UND var=a
o option „-n“ – kein Zeilenumbruch

• expr 3 + 2 ODER echo $((3 + 2)) Rechnen (Leerzeichen nicht vergessen!!)


• date +%Y-%m-%d -> für mehr man date
• exit 0 gibt exit-status an (0= alles gut, 1 = error)

• If Syntax:
o befehl meist „test Ausdruck“ Oder auch „[ Ausruck ]“
o wichtige Optionen: „-o“ – oder, „-z“ – leerer String?, „-e“ – exists? Weitere: man test
if befehl

then
befehl
else fi
befehl

Beispiel:
if [ "$eat" = apfel -o "$eat" = birne ] # oder: if test "$eat" = apfel
-o "$eat" = birne then

echo Ich liebe Äpfel und Birnen!

while-Schleife
Führe COMMANDSaus solange TESTerfolgreich ist:
while TEST do

COMMANDS
done
Beispiel:
i=10
# Wichtig: Leerzeichen!
while [ $i -gt 0 ] do

echo $i let
i=i-1
(Mit letkann man die Shell anweisen, arithmetische Ausdrücke auszuwerten, also zu
rechnen)

for-Befehl
Führe die COMMANDSfür jeden Wert in WORDSaus (iteriert über jedes Wort). Die Variable NAME

nimmt dabei den aktuellen Wert an (siehe etwa Beispiel bei D.2):
for NAME in WORDS do

COMMANDS
done
Beispiel:
for var in Osten Süden Westen do

echo Die Sonne steht im $var done

Wird WORDSnicht angegeben so wird $@(= alle Parameter des Skripts, also $1 $2 $3…)

verwendet.
Beispiel: Dateien in Verzeichnis abarbeiten
for datei in $(ls *.txt) # Kommandozeilensubstitution $(...) siehe nächster
Abschnitt!

do
h T d i f d $d id
Beispiel: Zählschleife
for counter in {10..1..-1} # Syntax: {START..END..INCREMENT} do

echo "--> $counter" done

echo " *** BLAST OFF ***"

CMD1 && CMD2 CMD2 wird ausgeführt wenn CMD1 erfolgreich war

CMD1 || CMD2 CMD2 wird ausgeführt wenn CMD1 nicht erfolgreich war

Get File Extension / Filename

a=$1
b="."

ending=${a/*$b/$b}

justName=${a/$b*}
Nützliche Befehle für Scripts und Pipes

expr, eval, $(( Rechnen in der Shell


)), let

$( … ) Textausgabe der Befehlskette in Klammern als Text substituieren, z.B.:


echo hallo > $(date +%Y%m%d).log

Alternativ gibt es eine Variente mit Backticks, die schlechter lesbar ist und
vermieden werden sollte: echo hallo > `date +%Y%m%d`.log
cut Ausschneiden von Spalten der Eingabe
head, tail Ersten 10 bzw. letzten 10 Zeilen einer Datei ausgeben
• Option „-n i“ (i=Zahl), gibt die letzten i-Zeilen aus
sort Sortieren
uniq Zusammenfassen gleicher Zeilen der Eingabe
grep Suche nach Suchmustern
wc Zählt Zeichen, Wörter ("Word Count), Zeilen….
basename, dirname Verzeichnis und Suffix von Dateinamen verarbeiten

Alternativ: ${var// … / … } oder $(var#…}


read Einlesen eines/mehrerer Werte von der Standardeingabe in Variable, z.B.:
read a b c

echo erster Parameter ist $a, zweiter ist $b, Rest ist in
$c

awk Muster in der Eingabe erkennen und verarbeiten (rechnen), kann auch zum
Verarbeiten von “Spalten” in der Eingabe verwendet werden, z.B.
echo 2 + 3 | awk { erg=$1+$3; print $erg }

sed Streameditor zum Filtern und Umwandeln von Text, z.B. (search/replace):
echo 15% | sed s/%//g

exit Beenden (mit Rückgabewert), z.B. exit 0 bei Erfolg, exit 1 bei Fehler.
Den Wert des Exit-Status eines Scripts oder Programms kann man in einem
Script abfragen (mit test!)

convert Wandelt file je nach option um (z.B. resize )


gzip Zipt ein file/Ornder
read
liest von stdinput ein in String
Scripte

• Farbtausch
#!/bin/bash

if [ $# = 2 ]
then

for file in $(ls *.png)


do
if [ $2 = "transparent" ]
then
convert $(basename $file) -transparent $1 "xchg_$(basename ${file})"
else
convert $(basename $file) -fill $2 -opaque $1 "xchg_$(basename
${file})"
fi
done

else
f=$(basename $3)

if [ $2 = "transparent" ]
then
convert $f -transparent $1 "xchg_$f"
else
convert $f -fill $2 -opaque $1 "xchg_$f"
fi

fi

• Brauchmanet:
#!/bin/bash

cd $1

for file in $(ls -S | tail -n $2)


do
if ls $file | grep -q "brauchmaschon" #[ $(basename ${file} ) !=
brauchmaschon" ]
then
false
else
rm $file
fi
done
exit 0
• Streifen:
#!/bin/bash
echo -n
"Laenge:" read
answer
i=0
while [ $i -lt
$answer ] do
echo
"@"
let
i=i+1
done
while
true
i=0
do
echo -n
"Nochmals?"
read answer2
if [ $answer2 =
"nein" ] then
exit 0
elif [ $answer2 =
"ja" ] then
echo -n
"Laenge:"
read
answer
while [ $i -lt
$answer ] do
echo
"@"
let
i=i+1
done
else
echo "Falsche Antwort!"
f
i
d
o
n
e

You might also like