Introduction To Bash Script
Introduction To Bash Script
Introduction To Bash Script
Scripts
A Bash script is a plain text file which contains a series of commands. These
commands are a mixture of commands we would normally type ourselves on the
command line (such as ls or cp for example) and commands we could type on the
command line but generally wouldn't (you'll discover these over the next few pages).
Running a Bash script is fairly easy. Another term you may come across
is executing the script (which means the same thing). Before we can execute a
script it must have the execute permission set (for safety reasons this permission is
generally not set by default).
If you forget to grant this permission before running the script you'll just get an
error message telling you as such and no harm will be done.
Why the ./
You've possibly noticed that when we run a normal command (such as ls) we just
type its name but when running the script above we put a ./ in front of it.
When you just type a name on the command line Bash tries to find it in a series of
directories stored in a variable called $PATH. We can see the current value of this
variable using the command echo.
Bash only looks in those specific directories and doesn't consider sub
directories or your current directory. It will look through those directories in order
and execute the first instance of the program or script that it finds.
The $PATH variable is an individual user variable so each user on a system may
set it to suit themselves.
/home/smahmud74/bin
• This allows me to put my own scripts and programs there and then I can use
them no matter where I am in the system by just typing their name. I could
even create a script with the same name as a program (to act as a wrapper) if I
wanted slightly different behaviour.
#!/bin/bash
This is the first line of the script above. The hash exclamation mark ( #! )
character sequence is referred to as the Shebang. Following it is the path to the
interpreter (or program) that should be used to run (or interpret) the rest of the
lines in the text file. (For Bash scripts it will be the path to Bash, but there are
many other types of scripts and they each have their own interpreter.)
Formatting is important here. The shebang must be on the very first line of the
file (line 2 won't do, even if the first line is blank). There must also be no spaces
before the # or between the ! and the path to the interpreter.
As we saw above, formatting for the shebang was important (ie no spaces, must be
on first line). There are many areas in Bash scripts where formatting is important.
Typically it involves spaces and either the presence or absence of a space can be the
difference between the command working or not.
I'll point these out as we encounter them. Also get in the habit of being mindful of the
presence or absence of spaces when looking at code.
To read the variable we then place its name (preceded by a $ sign) anywhere in the
script we would like. Before Bash interprets (or runs) every line of our script it first
checks to see if any variable names are present. For every variable it has identified,
it replaces the variable name with its value. Then it runs that line of code and begins
the process again on the next line.
When we run a program on the command line you would be familiar with supplying
arguments after it to control its behaviour.
For instance we could run the command ls -l /etc. -l and /etc are both command line
arguments to the command ls.
We can do similar with our bash scripts. To do this we use the variables $1 to
represent the first command line argument, $2 to represent the second command
line argument and so on. These are automatically set by the system when we run our
script so all we need to do is refer to them.
This is one of those areas where formatting is important. Note there is no space on
either side of the equals ( = ) sign. We also leave off the $ sign from the beginning of
the variable name when setting it.
Quotes:
In the example above we kept things nice and simple. The variables only had
to store a single word. When we want variables to store more complex values
however, we need to make use of quotes. This is because under normal
circumstances Bash uses a space to determine separate items.
Because commands work exactly the same on the command line as in a script
it can sometimes be easier to experiment on the command line.
When we enclose our content in quotes we are indicating to Bash that the
contents should be considered as a single item. You may use single quotes
( ' ) or double quotes ( " ).