Skip to content

Instantly share code, notes, and snippets.

@laic
Last active November 11, 2022 16:22
Show Gist options
  • Save laic/407eb25ec1114f4d1bb0a156379a5bfc to your computer and use it in GitHub Desktop.
Save laic/407eb25ec1114f4d1bb0a156379a5bfc to your computer and use it in GitHub Desktop.
A shell scripting primer by Jason Fong

A Bash Shell scripting Primer

Credit to Jason Fong (a few edits by Catherine Lai)

find out current location in disk (stands for "print working directory")

pwd

list contents of current directory

ls 

list contents of current directory with more information, and in list format

ls -l 

make a directory

mkdir new_dir

change into a directory

cd new_dir

leave the directory (go back up one level)

cd ..

remove the directory

rmdir new_dir

create blank file

touch new_file

write contents to the file: Note > will overwrite whatever was in that file before, >> does not overwrite contents of file but rather appends it to the end

echo hello world > new_file  
echo hello world2 >> new_file

print contents of file to terminal

cat new_file

browse contents of file

less new_file

while browsing using less you can use the following keyboard shortcuts:

  • q to quit
  • j,k to navigate up and down
  • u,d to navigate pages up and down

search for the string "hello" in file

grep "hello" new_file

redirect output of a command to another new file

grep "hello" new_file > newer_file 

overwrite contents of file

echo bye world > new_file # > does overwrite contents of the file

create a bash file using a text editor

touch test.sh #touch creates a file if one doesn't exist

check read (r), write (w) and execute (x) permissions of the test.sh file

ls -al 

type man ls on the command line to examine what the flags '-a' and '-l' do

add a bash command to the file

echo "echo 'hello world'" > test.sh 

The > redirects the output of the command to a file

examine the contents of this file

less test.sh

execute the file

bash test.sh

add another line to the file

echo "echo 'bye bye world'" >> test.sh 

The >> tell the shell to concatenate the output of the command to the file test.sh but doesn't overwrite it

add executable permission to it

chmod a+x test.sh 

now we can execute this file using dot notation

./test.sh
  • add 'sha-bang' (i.e., #!) to the file so that it's interpreted as a bash script
  • use a text editor to open the file and add #!/bin/bash to the very top or use "vim test.sh" or "nano test.sh" to edit it

how to create a variable

var1='hello world'
var2=helloworld
var3=999
var4=$var3 #can assign a variable to the contents of another variable
var5=${var4}appendedtext #can use curly brackets to clarify variable names

Now have a look at what the values we've assigned to these variables

echo var1 is $var1
echo var2 is $var2
echo var3 is $var3
echo var4 is $var4
echo var5 is $var5

Some examples of for loops over files

touch 1.txt 2.txt 3.txt #make a few dummy files for demonstration

In a bash script, you can write a for loop over multiple lines like so:

for filename in ./*; do #loop over files in current directory
    echo $filename #* is a wildcard that matches anything
done

You can also write it all in one line if you want to run it on the command line (note the use of semicolons ;):

for filename in ./* ; do echo $filename ; done

Some more example for loops:

loop over files with given extension

for filename in *.txt; do 
    echo $filename
done

loop over files in the parent directory, .. is an alias for the parent directory)

for filename in ../*; do 
    echo $filename 
done

how to loop over numbers: a C++ style for loop which iterates over variable i from 0 to 4 (stop at 5)

for ((i=0; i<5; i++)); do 
    echo $i
done

Another example: use the seq command

for j in $(seq 5 10); do 
    echo $j
done

how to call a script from within a for loop (e.g. in another script)

for ((i=0; i<5; i++)); do
    ./test.sh
done

how to call a script with arguments: First let's create a script called test2.sh that writes out "printing number" and then the first and second command line arguments (i.e., whitespace separated strings that are written immediately after the command) $1 refers to the first command line argument, $2 refers to the second.

touch test2.sh
chmod a+x test2.sh                                 # make it executable
echo "echo 'printing number' \$1 \$2" > test2.sh   # $1 refers to first command line argument

Now put the script into a for loop.

#note: we use \$ to escape the dollar sign
for ((i=0; i<5; i++)); do
    ./test2.sh $i wow
done

It should print out:

printing number 0 wow
printing number 1 wow
printing number 2 wow
printing number 3 wow
printing number 4 wow

how to iterate over the lines contained within a text file

touch test3.txt
chmod a+x test3.txt
echo 'line1' >> test3.txt
echo 'line2' >> test3.txt
echo 'line3' >> test3.txt
echo 'line4' >> test3.txt
echo 'line5' >> test3.txt
for line in `cat test3.txt`; do #note: we are using `! not ' or "...
    echo $line
done

be careful using single and double quotes, they have different behaviours when expanding variables

echo 'var4 contains $var4'
echo "var4 contains $var4"

if statement - check if two strings are equal

boolvar=lol101
if [ $boolvar = lol101 ]; then #make sure to add the spaces around the square brackets
    echo 'yes'
else
    echo 'no'
fi

Another if example

if [ $boolvar = should_echo_no ]; then #make sure to add the spaces around the square brackets
    echo 'yes'
else
    echo 'no'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment