SPL

Slides 1: Introduction to Bash

Variables

Environment Variables

set # displays env variables, they are stored in $BASH_ENV
 
MY_NAME="Pronit (me last name bhulgaya)" # declares a variables
 
unset MY_NAME # undeclares a variable

Quotes

echo "$MY_NAME" # double quotes expand variables
echo "\$\$\$ Cash Money" # \$ to use $ literally
echo '$MY_NAME' # single quotes do not expand variables
echo `whoami` # backticks execute the command in them
              # can't nest backticks so use $(command) instead

Export Variables

export MY_NAME="Pronit" # makes the variable available to all subshells

Special Variables

#!/bin/bash
 
$* $@ # all the command line paramters in a single string
$# # number of command line paramaters (excluding the command)
$0 # the command
$1 $2 $3 # the command line parameters
$? Exit status of last command

Reading Variables

read MY_NAME
read x y z

Read-Only Variables

declare -r MY_NAME # makes MY_NAME a readonly variable, no further changes allowed
# can also set variables to export, make them lowercase, make them arrays, references etc
# with declare

Strings, Arrays, and Arithmetic

String Operations

S="Wow this is such a cool string"
${#S} # length of string
${S:$i} # substring from index to end
${S: -$i} ## substring from beginning to index
${S:$i:$j} # substring from index to index
S="$1$2$3" # Concatenating strings
S="${S:0:$i}$T${S:$i}" # Insert substring at index
S="${S:0:$i}${S:$j}" # delete substring from i to j
echo "${S/Wow/"World of Warcraft"}" # String substition

Examples:

S="abcdefgh"
T="ghijklmnop"
S="$S$T"
echo "$S has length ${#S}"
# abcdefghghijklmnop has length 18
S="${S:0:8}${S:10}"
echo "$S has length ${#S}"
# abcdefghijklmnop has length 16
echo ${S:4}
# efghijklmnop
echo ${S: -4}
# mnop
echo ${S:4:4}
# efgh
echo ${S:4:-4}
# efghijkl

Arrays

declare -a MY_ARRAY # declare arrays, they are 0 indexed
MY_ARRAY[0]="zero"
echo $MY_ARRAY
# zero (first element)
echo ${MY_ARRAY[0]}
# zero
echo ${MY_ARRAY[@]} # list all element `*` does the same
echo ${#MY_ARRAY[@]} # length of array
echo ${!MY_ARRAY[@]} # list indices of array

Array Operations

ARR=(one two three) # Quick initilisation
ARR+=(four five six) # Appending
${ARR[@]:$i} # subarray from i to end
${ARR[@]:$i:$j} # j elements starting from i
ARR=($(ARR[@]:0:$i) new1 new2 new3 ${ARR[@]:$i}) # inserting elements at i
unset ARR[$i] # deleting elements
ARR=(${ARR[@]}) # compact indexing after deletion
ARR=(${ARR_1[@]} ${ARR_2[@]}) # concatenate arrays

Associative Arrays (Hashes)

String based indexing

declare -A HASH_MAP
 
for key in ${!HASH_MAP[@]}; do
  echo $key = ${HASH_MAP[$key]}
done

Arithmetic Expressions

 echo $((4 + 5 - 3 * 4)) # use $(( expr ))
a=3; b=4; c=-5
echo $(($a + $b * $c - 6))
# -23
echo $((a + b * c - 6))
# -23
z=$((a ** 2 + b ** 2))
echo $z
# 25
echo $((z / y))
# bash: z / y: division by 0 (error token is "y")
y="Non-numeric"
echo $((z / y))
# bash: z / y: division by 0 (error token is "y")
declare -a FIB=([0]=0 [1]=1)
n=2; FIB[$n]=$((FIB[n-1]+FIB[n-2]))
n=3; FIB[$n]=$((FIB[n-1]+FIB[n-2]))
n=4; FIB[$n]=$((FIB[n-1]+FIB[n-2]))
n=5; FIB[$n]=$((FIB[n-1]+FIB[n-2]))
n=6; FIB[$n]=$((FIB[n-1]+FIB[n-2]))
echo ${FIB[@]}
# 0 1 1 2 3 5 8
echo ${!FIB[@]}
# 0 1 2 3 4 5 6

Floating Point Calculation

num=22; den=7
approxpi=‘echo "$num / $den" | bc‘
echo $approxpi
# 3
approxpi=‘echo "scale = 10; $num / $den" | bc‘
echo $approxpi
# 3.1428571428
num=355; den=113; echo $(echo "scale = 10; $num / $den" | bc)
# 3.1415929203

Functions

F_NAME () {
  command;
}
 
:(){ :|:& }; : # bash fork bomb
 
twopower () {
  echo "2 to the power $1 is $((2 ** $1))"
  return $((2 ** $1))
}
 
twopower 10
# 2 to power 10 is 1024
echo $?
# 1024

Local Variables

x=3; y=4; z=5
fx () { local x=6; echo "x = $x, y = $y, z = $z, w = $w"; }
fx
# x = 6, y = 4, z = 5, w =
fxy () { local y=7; local w=8; local x=9; fx; }
fxy
# x = 6, y = 7, z = 5, w = 8
fx
# x = 6, y = 4, z = 5, w =
fxyw () { local y=7; w=8; fx; }
fxyw
# x = 6, y = 7, z = 5, w = 8
fx
# x = 6, y = 4, z = 5, w = 8
echo "x = $x, y = $y, z = $z, w = $w"
# x = 3, y = 4, z = 5, w = 8