Notes on shell scripting.

Bash is a Unix shell and command language originally released in 1989. It can read and execute text files called shell script that are useful for automating tasks.

The terminal cheatsheet contains some of the commands that can be run as part of a bash script.

Running a script

Navigate to the directory containing the script and use ./ to execute it.

If the script fails with an permissions error, use chmod +x to make it executable.

#! Shebang. Indicates which interpreter a script should be run with: #!/bin/bash
$1, $2, ... The first, second, etc command line arguments to the script.
variable=value To set a value for a variable. Remember, no spaces on either side of the equal sign

Conditional logic

# conditional logic
if [ -d $HOME ]; then
  echo "The home directory path is $HOME"
  echo "Couldn't find home directory!"

# use semi colon when writing one liners
if [ -f $file ]; then echo "Found $file"; fi

# use double brackets to combine conditions
if [[ $condition == true && ! -n $value ]]; then
	echo "The $condition is true and value was not set."

# comparisons
if [[ $value -gt 0 && $value -lt 100 ]]; then
	echo "$value is greater than 0 and less than 100"


# print variable to text file overwriting existing content
echo $string > $file

# append a string to a text file
echo $string >> $file

# store file content as variable
string=$(cat $file)

# read from stdin
read -p "> " $string
echo "You entered $string"

# print the length of a string

# replace substring
string="The wind came from North East"
string=${string/"East"/"West"} # "The wind came from North West"

# find substring in string
if [[ $subString == "$string"* ]]; then 
  echo "$subString was found at the beginning of $string"
elif [[ $subString == *"$string"* ]]; then 
  echo "$subString was found in the middle of $string"
elif [[ $subString == *"$string" ]]; then 
  echo "$subString was found at the end of $string"
  echo "$subString was not found in $string"

# manipulating filenames
local file=$(basename $path)   # file.txt
local fileName="${file%%.*}"   # file
local fileExt="${fileName#*.}" # ext

# font formatting
bold=$(tput bold)
normal=$(tput sgr0)
echo "${bold}This text is bold.${normal}"


# define an array
array=( "item1" "item2" "item3" )

# get the size of an array
echo ${#array[@]}

# loop through array items
for item in ${array[@]}; do
  echo $item

# read words from file and store them in an array
# read from file
while read line; do
  let count++
done < $file
# read from array
for line in "${words[@]}"; do
  echo ${line}


# reads a text file line by line
while read line; do
  echo $line
done < $file

# count files in folder
for $file in $path/*; do
  echo $file
  let count++

# print files of type txt
for $file in $path/*.txt; do
  echo $file

### Snippets
# use bash parameter expansion to set default variable values

# start a program as a forked process in a new window
programname &

# erases the last line printed in the terminal
tput cuu1;tput el;

# stopping time for script execution duration
	startBuild=$(date +%s,%N)
	_s1=$(echo $startBuild | cut -d',' -f1)   # sec
	_s2=$(echo $startBuild | cut -d',' -f2)   # nano sec
	# run commands
	end_at=$(date +%s,%N)
	_e1=$(echo $end_at | cut -d',' -f1)
	_e2=$(echo $end_at | cut -d',' -f2)

	# calculate passed time in seconds
	passedTime=$(bc <<< "scale=3; $_e1 - $_s1 + ($_e2 -$_s2)/1000000000")
	echo "The process took $passedTime seconds."


Use set with the following parameters for easier debugging:

e Halts on the first error.
u Halts on unset variables.
o -pipefail Halts on errors in pipeline.
v Prints each line before it is executed.
x Prints each line after all substitutions and expansions have been performed.

Evaluating the arguments passed to a script

# case example
case $1 in
  echo starting
  echo stoping
  echo restarting
  echo don\'t know


incoming(11) | relogen | logbook | memoir | scim | meta | trabant | tablet | printer | photography | terminal | rsync