Bash Simplified Guide and Cheat Sheet

This is a quick and dirty guide for bash and Linux with some must understand concepts. Consume at your own risk.

Everything after a # sign except within the context of a quote or double quote

Programs or executables are scripts or binaries that perform actions. Programs have the following:

  1. Parameters # echo “abc” “ef” where echo is the 0th parameter, abc is the 1st, ef is the 2nd.
  2. IO Streams # usually have 3 streams, input, output, and error but can have more.
  3. filesystem read and write access # the everything is a file unix philosophy
  4. ioctl’s # IO controls is for device control which is beyond the scope of this doc

Variables are string type except when they are arrays or maps.

a=1 # string "1"
a='1' # single quotes prevents substitution of variables
a="1" # double quotes allow substitution of variables
echo $a # prints 1 with a new line to output stream
echo '$a' # prints $a with a new line to output stream
echo "$a" # prints 1 with a new line to output stream

Why use double quotes?

b=$a  b # two or more whitespace characters collapse into one, "1 b"
a='$a  b' #preserves whitespace and does not allow substitution, "$a  b"
a="$a  b" #preserves whitespace and allows substitution, "1  b"

IO Streams

  • Input can be your keyboard, or stream coming out of another program.
  • Output is the stream coming out of your program. For echo, it is the content being printed.
  • Error is another stream like output but for errors and other messages not wanted in the output.

Bash has a few ways to manipulate streams:

  • “|” “pipe” connects the output stream of the left program to the input of the right program
    • echo “abc” | sed s/b/d/ # send “abc” to sed and replace b with d, “adc”
  • “>” “redirect to file” sends the output stream of the left program to the file on right
    • echo “abc” > hello.txt # sends “abc” to a file named hello.txt
    • if the file does not exist, create it. if it does exist, overwrite it.
  • “>>” “redirect append to file” appends stream of the left program to the file on the right
    • echo “ef” >> hello.txt # appends “ef” to a file named hello.txt
    • if the file does not exist, create it. if it does exist, append to it.
  • “<” “redirect from file” reads the right file and redirects the stream to the input of the left program
    • cat < “hello.txt” # reads from hello.txt and prints its content “abcdef” to output stream
  • At the end of a command chain, if output and error streams are not directed anywhere, bash assumes you want to print the output to your terminal.

Basic Programs

  • echo # reads the parameters and send them to output stream
    • echo “abcd” # abcd on output stream
  • ls # reads parameters, list filesystem, and send file names to output stream
    • ls / # list contents of / (root) and sends file names to output stream
  • cat # if parameters, treat each parameter as file, read them, and send them to output stream. if no parameters, read input stream and send to output stream
    • cat hello.txt # sends contents of hello.txt to output
    • cat hello.txt > hello_copy.txt # reads contents of hello.txt and redirect the output to file hello_copy.txt
  • sed # reads input stream, does substitution based on parameter 1 regex, sends to output stream. if parameter 2 is defined, read from file instead of input stream
    • sed s/search/replace/ # replace the first instance of “search” with “replace”
    • sed s/search/replace/g # replace all instances of “search” with "replace
    • sed supports basic regex and with the -E switch, more extended regex features
  • tr # reads the input, does character substitution based on first two parameters, sends to output

Conditional Flow Control

if [ $a -eq 1 ]; then # integer equivalence comparison only
  : # blank or text in variable will cause this to fail
elif [ $a -gt 1 ]; then # -gt greater than, -lt less than,
  : # -ge greater than equal to, -le less than equal to 
elif [ "$a" = "a" ]; then # text comparison
  : # use double quotes if variable can be empty
  : # if you're not doing anything, you must put : here
case $i in
  a) # matches i=a
  a*) # matches anything starting with a
  *) # optinal wildcard match if i does not match above

Iterative Flow Control
For loops:

for i in a b c; do
  echo $i
done # this will print a, b, c on each line
for i in $(seq 0 9); do # $(command) runs a subshell and returns the output
  echo $i
done # this will print 0 through 9 on each line

While loops:

while true; do # use forever loop
  if [ $i -eq 10 ]; then
     break # end forever loop
  i=$((i+1)) # print i + 1 in subshell and assign output to i
while [ i -gt 0 ]; do # use conditional loop
  #do something
  i=$((i-1)) # without this, this loop will run forever


Variable Substitution


Special Devices

  • /dev/null #useful for dumping stuff you don’t need, eg. cat hello.txt > /dev/null
  • /dev/zero #useful for getting an endless input stream, eg. cat /dev/zero | tr “\0” “a”
  • /dev/urandom #useful for getting an endless random input stream
1 Like