• Numerical operators -eq, -ne
# Checking if variable ≡ 2 print "yes"
if [ $varname -eq 2 ]; then echo "yes"; fi

# Checking if variable ≠ 1 print "yes"
if [ $youngone -ne 1 ]; then echo "yes"; fi

  • Logical Not !

To negate an expression: ` ! expression ` OR ` [ ! expression ] `


if [ ! $var1 -ne $var2 ]; then echo "$var1$var2"; fi

- Filetype operator `-f`

# Print "File" if filename is a file (not directory)
if [ -f $filename ]; then echo "File"; fi


Using the $ character introduces parameter expansion, command substitution, or arithmetic expansion.

  • Parameter Expansion
  • Arithmetic Expansion - evaluates as numbers not strings
  • Brace Expansion - allows you to generate arbitrary strings (similar to pathname expansion)
  • Command Substitution - allows command output to replace command name
bash Result
${parameter} value of parameter is substituted
${parameter:offset:length} Substring expansion expands up to length (default EOS) of characters starting at offset
$(( expr )) evaluates expr as a math expression
$(command) executes command in subshell and replaces in stdout of command

Examples: echo $(cat file) or the faster $(< file)

Data Structures


Ordered collections of objects.

Arrays: Array=()

  • appending items to an array
arr+=('DJ Khaled')
arr+=('Another One!')

Command Line Arguments

You can use $<number> to access command line arguments and $# to access the number of arguments given. Of course, the shell script you’re in is the zero-th argument.


# checks that user entered 2 args
if [ $# -ne 2 ]
	# complain and quit
	echo "Usage: $0 arg1 arg2"
	exit 1

Specific Command

  • Delete all files except certain file types
  • Testing return value of find function if find . -name ‘abc’ -type f -exec grep -q xyz {} + then : All invocations of grep found at least one xyz and nothing else failed else : One or more invocations of grep failed to find xyz or something else failed fi

  • Delete files older than x Days Command syntax - =find /path/to/files* -mtime +5 -exec rm {} \;=
    • cmd: find
    • path to files
    • specify with mtime deleting files /older/ than 5 days
    • exec allows you to pass rm

• Basic enabling script • chmod +x • Links to look at… • — Arch Linux Handy Cmd Line Scripts

  • for loops and arrays:
    • iterators:
      • `”${arrayName[@]}” - all elements, space separated
        arr = ("hello" "world")
        for i in "${arr[@]}"; do
        echo $i
  • iterate over files and filenames print files
arr=("hw1" "hw2" "comics")
for filename in /home/cam/Downloads/*.pdf; do
   echo $filename
   for target in "${arr[@]}"; do
      if [[ $filename == "$target.pdf" ]] then
         echo "found target $target"

Check if device is mounted

if [ $( mount | grep -c $device ) != 1 ]; then
	echo "Device not mounted!"
	echo "Device mounted!"

Reading from a a file and printing

while IFS= read line
    # display or do something with $line
    echo "$line"
done <"$file"

Copying file and renaming into folder

  • read filenames from file
  • copy files into list.txt folders
# Script for grading CS104 files since their python script wasn't working for me
while IFS= read -r username
    echo "Copying file $report and moving to folder $username"
    cp "GR${hw}" "$report"
    mv "$report" "./$username"
done < "$input"

Replace Spaces with Underscores in Dir’s Filenames

for f in *;do mv "$f" "${f// /_}";done

Random Values

/dev/urandom used to generate random values cryptographic security ` echo $RANDOM generates random number from **0** => **16* * **(2^31 - 1)** echo $(( 1 + RANDOM % 100 ))` get random number between 1 -> 100

  *print folder names* ```bash for filename in /home/cam/*; do    echo $filename done ```    - `if`, `else` and conditionals:
  - strings `[[STRING == STRING]]`
  - numbers `[[NUM -eq NUM]]` ## xargs
  • doesn’t read bash alias
  • reads independent scripts