Shell scripting is essential if you are more than a regular user of a Unix-like operating system. Most Linux systems come with Bash, the Bourne-again shell, and that’s why I chose Pro Bash Programming: Scripting the Linux Shell, by Chris Johnson, to learn more about it. (this entire phrase is kinda stupid, lol)

This book covers basic to advanced topics in little more than 200 pages (of actual text), and even though everything is a little condensed, the text is clear enough to understand. All the basics are covered, and if you want to stick only with them, read up to chapter 6 and skim through the rest of the book - you won’t really miss learning how to “implement” awk in Bash. ;)

Despite many open-source books on Bash being available on the interwebz (Advanced Bash-Scripting Guide, Bash Guide for Beginners), Pro Bash Programming is much more newbie friendly, and I highly recommend it.

Book on Amazon: http://www.amazon.com/Pro-Bash-Programming-Scripting-Experts/dp/1430219971

## My Personal Notes

### Chapter 1 - Your first shell program

• A shell script contains one or more commands that you would type on the command line.
• type <cmd> tells you what the shell will execute.
• When a shell is given a command to execute, it looks for it in the $PATH directories. • Commands are usually stored in a directory named bin. • Comments: # lorem ipsum • Shebang: tells the system which interpreter to use. #!/bin/sh #!/bin/env bash  • Execute script in the current shell environment: source file.sh # or . file.sh  •$PWD: path of the shell current directory.
• $HOME: path of user directory. •$PATH: colon separated list of directories in which command files are stored.

### Chapter 2 - Input and output

• A value of a parameter can be accessed by preceding its name with a dollar sign ($foo,$bar).
• Positional parameters are available as numbered parameters.
printf "Hello, %s!" "$1"$ hello.sh John
$Hello, John!  • Variables var=Foo echo$var
$Foo  • Formatting and printing data is done by printf. printf <FORMAT (%s %d %f %e)> <ARGS> printf "%s" Hello World$ Hello World

• IO streams and redirection
• There are three IO streams attached to every command: standard input, standard output and standard error.
• Redirection
• > creates or truncates a file
• » appends to a file
• < reads from file (or standard input)
# from stdin

# from file

• Pipelines: they connect the standard output of one command directly to the standard input of another.
• Command subsitution: the output of a command can be stored in a variable.
today=$( date ) curpwd=$( pwd )


### Chapter 3 - Looping and branching

• Exit status: it’s stored in the special variable $?. $? == 0  -> success
$? != 0 -> error  • Testing an expression: they can be tested with [[ ]]. # Test a file [ -x$HOME/file ]

# Test an integer
# OPERATOR: -eq, -ne, -gt, -lt, -ge, -le
[ 2 <OPERATOR> -1 ]

# Test string
[ "a" = "b" ]
[ "a" != "b" ]

# Test regex
# The regular expression must be unquoted
[[ $string =~ h[aeiou] ]]  • Conditional execution if <condition list>; then elif <condition list>; then else fi  • Conditional operators: list containing conditional operators are evaluated from left to right. # Test of dir exists and then cd into it test -d "$dir" && cd "$dir" # Exits with an error if CD fails cd "$dir" || exit 1

• case compares a word against one or more patterns:
case $str in pattern1) commands;; pattern2) commands;; esac  • Looping while <commands>; do done until <commands>; do done for var in Canada Usa Uk; do printf "%s\n" "$var"
done


### Chapter 4 - Command-line parsing and expansion

• The special parameter [email protected] expands to a list of all command-line arguments.
• Parsing options: single characters preceded by a hyphen can be passed with getopts.

### Chapter 6 - Shell functions

• A function is a compound command that has been given a name.
function name()
{
<commands>

# Restricts scope of variable to function
local var=Foo
}



### Chapter 8 - File operations and commands

• cat: reads all file arguments prints their contents to standard input.
• head: prints first 10 (or n) lines of a file
• touch: updates the timestamp of a file, and creates one if it doesn’t exist.
• cut: extracts portions of a line.
• wc: counts the number of lines, words and bytes in a file.
• grep: searches files or the standard input if files are not given, and prints lines matching a string or regex.