Introduction to Shell Scripting
Welcome to our shell scripting tutorial series! It's required that you have a basic knowledge of the Linux Command Line, so be sure to review it before you proceed.
What is a shell script?
A shell script is a file containing lines of code for the shell to execute. The shell reads each line as if you typed them in one at a time into the terminal.
Scripting languages are interpreted, meaning that they aren't compiled into machine language. This results in less efficiency, but in exchange, we are able to more easily write our scripts.
The scripts may be written in any text editor, so simply select one with syntax highlighting - you may use Vim within your terminal, or something like Sublime Text.
Learning a text editor
We recommend that you learn a text editor while learning shell scripting. Additionally, if you want to try out Vim, head on over to our in-browser Vim tutorial series, or type vimtutor
into the command line. Taking the time to learn Vim keys will speed up your workflow immensely.
When not to use shell scripts
Remember that shell scripts should be short and used to manipuate files and commands. If you find that you're writing something that looks overly complex, and involves a slew of string or arithmetic operations, look into a better-suited scripting language such as Python or Perl.
Quick syntax guide
For this module, we'll be using both the command line and shell scripts to give examples of what we're learning.
Anything that comes after a $
is considered a command to be typed out on the command line. Anything following a >
symbol is a continuation of the line before.
Comment and explanation lines preceed with a #
.
$ cd ~
# Change to home directory
$ echo "Anything proceeding a > \
> symbol means it's a continuation \
> of the line before."
Anything proceeding a > symbol means it's a continuation of the line before.
Lastly, anything without a preceding special character will be a snippet from a shell script file.
echo 'This is a snippet from a shell script'
Great start! Now let's move on to some important symbols covered in shell scripting.
Important Symbols #, #!, ;, \
Let's go over some basic but important symbols encountered in a shell script.
Comments #
Commenting is performed with the #
symbol. Anything written after a #
symbol is ignored by the interpreter. Make sure to comment sufficiently throughout your scripting process!
#!shebang
The first line of a shell script may look like a comment since it starts with a pound symbol (#
), but it's actually a shebang.
The shebang symbol indicates where to find the program that will be used to execute the script. Thus, the following line of code would indicate that the sh
program (default shell executor) should be used to run the shell script, which is found in the /bin folder.
#!/bin/sh
Semicolons ;
A new line character symbolizes the completion of a command. If, however, you want to mark the end of a command and start a new one on the same line, use the semi-colon ;
.
Backslash \
The backslash (\
) can be used to break up a command line if it gets too long. Here is a contrived example:
$ echo hello world; echo \
> how \
> are \
> you
hello world
how are you
Our first shell script
Now let's make our very first shell script. First proceed by deciding on a name, and ensuring that it isn't already taken. We can do this with the which
command.
$ which hello_world
No output? Great! That means that hello_world
isn't already a command. Now let's create a normal file with the touch
command.
$ touch hello_world
Now open this file and place the following two lines inside the script:
#/bin/sh
echo 'Hello world!'
All set! Now all we have to do is run (or execute) the file. This can be done in several different ways, which we'll see in the next lesson.
A few ways to execute source, ., bash
Setting permissions
To run our script, we must first set its permissions so that we can execute it. We can do this with the chmod
command.
After our file mode is set to execute, we can then run the script with the source
command.
# Set permissions so user can execute
$ chmod u+x hello_world
# Check to see that it's executable
$ ls hello_world
-rwxr--r-- 1 JohnPC staff 151 Jul 2 18:21 hello_world
# Execute our file
$ source hello_world
Hello world!
Great job! You just ran your first shell script!
Let's now go through the various ways you can execute your script. The two main methods are with a command and as a command.
Executing with a command
We can easily execute our script with three different commands. The first, which we already saw, is the source
command. This takes as an argument the path name of our script, and executes it. A shortcut for source
is the period (.
). You may also use bash
command if you are using a bash interpreter or sh
for the regular shell.
$ source hello_world
Hello world!
$ . hello_world
Hello world!
$ bash hello_world
Hello world!
$ sh hello_world
Hello world!
All four commands perform the same task. This way of executing may seem convenient right now, but you can't execute it from anywhere on the file structure. Let's learn how to set up your system so you can execute as a command, which is more convenient in the long run.
Executing as a command
Oftentimes you'll want to execute your scripts as a command, just like every other command in the shell.
We can set this up in two ways:
- Place the script in a directory that's in our
$PATH
variable. - Add the folder containing our script in the
$PATH
variable.
Recall that the $PATH
variable contains all the folders the shell looks through when executing a command.
1) Adding script to a $PATH directory
To begin, let's use the echo $PATH
to check out which folders are in our $PATH
directory.
$ echo $PATH
/usr/local/git/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/JohnDoe/.composer/vendor/bin:/Users/JohnDoe/.composer:/usr/local/Cellar/postgresql/9.4.1_1/bin
You could technically add your script to any of your listed folders, but convention holds that we should list them in /usr/local/path. Now let's move our program to this folder.
$ mv hello_world /usr/local/bin
# Now the shell can access our program from anywhere on the file structure!
$ hello_world
Hello world!
2) Adding containing folder to $PATH
Another way to allow the shell to access our script is to add the containing folder to our $PATH
. Add the following code to your ~/.bash_profile. This is the file that gets run every time you start a new shell. Setting this up has to do with shell and environment variables.
PATH=$PATH:/path/to/our/scripts
export PATH
Great! Now restart your terminal, or simply run the .bash_profile file.
$ source ~/.bash_profile
$ hello_world
Hello world!
Great job!
PHEW! Okay, so hopefully all those methods don't overwhelm you. Note that executing as a command is more work to setup, but less work every time you access the script. Thus, it's important that you set up your $PATH
accordingly so that you can easily run the script from anywhere on your file structure.