Cogs and Levers A blog full of technical stuff

Bash Gems

The Bourne Again SHell is one of the most widely deployed shell for Linux that I use all the time. In today’s post, I’m going to collate a lot of the gems that I’d discovered in my travels of using this software.

Finding Help

Nothing can substitute the reference manual materials distributed with this software when it’s installed. At the console, you can read documentation in info format on bash using the following:

info bash

You’re able to deduce executing this command by doing some research at the console, by yourself. Using apropos (which searches the manual pages) you can look for key words.

If you wanted to find any command that begins with the characters ‘ls’ in an attempt to find the command ls, you can perform the following search:

apropos ls | grep '^ls.*'

On my system here, this function emits the following result:

ls (1)               - list directory contents
lsattr (1)           - list file attributes on a Linux second extended file s...
lsb_release (1)      - print distribution-specific information
lsblk (8)            - list block devices
lscpu (1)            - display information about the CPU architecture
lsdiff (1)           - show which files are modified by a patch
lsearch (3)          - linear search of an array
lseek (2)            - reposition read/write file offset
lseek64 (3)          - reposition 64-bit read/write file offset
lshw (1)             - list hardware
lsinitramfs (8)      - list content of an initramfs image
lslocks (8)          - list local system locks
lsmod (8)            - Show the status of modules in the Linux Kernel
lsof (8)             - list open files
lspci (8)            - list all PCI devices
lspcmcia (8)         - display extended PCMCIA debugging information
lspgpot (1)          - extracts the ownertrust values from PGP keyrings and l...
lstat (2)            - get file status
lstat64 (2)          - get file status
lsusb (8)            - list USB devices

We’re only interested in the first item there, but we’re given all of the options. We can now display the manual page with the following:

man ls 1

Variables

Variable creation is fairly straight forward:

# stores the string "John" in var1
var1="John"
# stores the text output of the command 'ls' into var2
var2=`ls -al`

# simple string replacement
var3=${var1/h/a}
# sub-string (turns "John" into "Jo")
var4=${var1:0:2}
# default string substitution (where null)
var6=${var5:-"Value for var5 was not supplied"}

# string interpolation is achieved with $
echo "His name is $var1"

Special variables exist to tell the developer a little bit about their environment:

Variable Description
$? Return code from the last program that just ran
$$ Currently executing script’s PID
$# Number of arguments passed to this script (argc)
$@ All arguments passed to this script
$1 $2 Each argument passed to the script ($3, $4, etc.)

Functions

# define a function
function syntax() {
  echo "usage: prog.sh [options]"
  return 0
}

function print_name() {
  echo "Hello $1"
  return 0
}

# call the function
syntax
print_name "John"

Control Flow Constructs

# Conditionals
if [ var1 == 10 ] then
  echo "It was 10"
else
  echo "It was not 10"
fi

case "$var1" in
  0) echo "Value was zero";;
  1) echo "Value was one";;
  *) echo "Anything but null";;
esac 

# Repetition
for var1 in {1..10} do
done

for ((x=1; x <= 10; x++)) do
done

while [ var1 == 10 ] do
done

Redirection

Special file descriptors of 0 as /dev/stdin, 1 as /dev/stdout and 2 as /dev/stderr.

From the redirections section in the bash manual:

Note that the order of redirections is significant. For example, the command

ls > dirlist 2>&1

directs both standard output (file descriptor 1) and standard error (file descriptor 2) to the file dirlist, while the command

ls 2>&1 > dirlist

directs only the standard output to file dirlist, because the standard error was made a copy of the standard output before the standard output was redirected to dirlist.