Skip to content

Shell scripts

Saagar Deshpande edited this page Sep 25, 2013 · 9 revisions

What is a shell script?

So far, all the commands you've been writing were only executed in the terminal. Often, however, you want to save the commands so that you can use them later. We can do this by writing a shell script! A shell script is essentially a sequence of commands that you would normally just type into the command line, but saved into a file. Let's take a look at our first shell script!

First, make sure you already have the source code for this bootcamp:

git clone git://
cd bootcamp-unix

There should be a scripts directory that will contain the examples from this part of tutorial. You'll be able to run all the examples from that directory.

cd scripts

Take a look at our first script (type cat to dump the contents of the file, and ./ to run the script).


echo "Hello bootcampers!"

Notice the #!/bin/bash at the very top. All shell scripts must start with this!!!. Also, if you run the command ls -l, you'll see this:

-rwxr-xr-x  1 kennyyu  staff  39 Sep 24 22:59

Notice the x's. All shell scripts must have executable permissions!!!. Remember that to add executable permissions to a file, use chmod +x FILE.


A shell script:

  • MUST start with #!/bin/bash as the very first line
  • MUST have executable permissions. Use chmod +x FILE to add executable permissions to a file
  • Can contain any sequence of commands that you can enter normally at the command line.
  • Each line must be its own complete command that should run in a terminal. A complete command can be chain of piped commands, or a sequence of commands separated by ;.
  • By convention, shell scripts have the .sh extension, but this is not necessary.

Reading Input

Let's move onto our next script where we will read input from the user:


echo "What is your name?"
read name
echo "Hi $name!"

When we run it (./, the read command prompts us for input, and it will save the input in the variable called name.

What is your name?
Hi Kenny!

Note that you can use pipes and input/output redirection like any normal command:

echo "Kenny" | ./ > hi.txt
cat hi.txt

This will send "Kenny" as input to the script, and all output of the script will be saved in the hi.txt file.


If we want to repeat an operation, we can use a loop. If you've programmed in C, C++, Java, Python, or any languages similar to these, you've probably seen the while and for keywords before. The terminal has both of these as well! We'll only discuss while for now. Let's take a look at the script


while read line
    echo "hi $line!"

If we run it (./, it will continuously prompt us for input until we press CTRL+D to signal the end of input:

hi Kenny!
hi Karen!
hi Saagar!

Everything between the do and done keywords is every time the while loop reads new input into the variable line.

Command Line Arguments

We can pass arguments at the command line to shell scripts! Take a look at the script


# Comments must start with the '#' character
# Command line arguments are found in $0, $1, $2, ...
# Note that $0 is always the name of the current program.

# '-z $1' will return true if the first argument is NOT set.
# '-o' will perform a logical OR
if [ -z $1 -o -z $2 ]
    echo "usage: $0 arg1 arg2"

echo "hello $1 and $2!"

If we run it without arguments (./, it will print the following message:

usage: ./ arg1 arg2

If we provide the arguments (e.g. ./ kenny saagar), it will run the program:

hello kenny and saagar!

Finish the rest of bootcamp

Go back to the main page.

You can’t perform that action at this time.