What Are Shell Scripts?
In the simplest terms, a shell script is a file containing a series of commands. The shell reads this file and carries out the commands as though they have been entered directly on the command line.
The shell is somewhat unique, in that it is both a powerful command line interface to the system and a scripting language interpreter. As we will see, most of the things that can be done on the command line can be done in scripts, and most of the things that can be done in scripts can be done on the command line.
Writing Your First Script And Getting It To WorkTo successfully write a shell script, you have to do three things:
- Write a script
- Give the shell permission to execute it
- Put it somewhere the shell can find it
Writing A ScriptA shell script is a file that contains ASCII text. To create a shell script, you use a text editor. A text editor is a program, like a word processor, that reads and writes ASCII text files. There are many, many text editors available for your Linux system, both for the command line environment and the GUI environment. Here is a list of some common ones:
Now, fire up your text editor and type in your first script as follows:
# My first script
echo "Hello World!"
If you have ever opened a book on programming, you would immediately recognize this as the traditional "Hello World" program. Save your file with some descriptive name. Eg: hello_world
The first line of the script is important. This is a special clue, called a shebang, given to the shell indicating what program is used to interpret the script. In this case, it is /bin/bash. Other scripting languages such as Perl, awk, tcl, Tk, and python also use this mechanism.
The second line is a comment. Everything that appears after a "#" symbol is ignored by bash. As your scripts become bigger and more complicated, comments become vital. They are used by programmers to explain what is going on so that others can figure it out. The last line is the echo command. This command simply prints its arguments on the display.
Setting PermissionsThe next thing we have to do is give the shell permission to execute your script. This is done with the chmod command as follows:
$ chmod 755 hello_world
The "755" will give you read, write, and execute permission. Everybody else will get only read and execute permission. If you want your script to be private (i.e., only you can read and execute), use "700" instead.
Putting It In Your PathAt this point, your script will run. Try this:
You should see "Hello World!" displayed. If you do not, see what directory you really saved your script in, go there and try again.
The shell maintains a list of directories where executable files (programs) are kept, and only searches the directories in that list. If it does not find the program after searching each directory in the list, it will issue the famous command not found error message.
This list of directories is called your path. You can view the list of directories with the following command:
$ echo $PATH
You can add directories to your path with the following command, where directory is the name of the directory you want to add:
$ export PATH=$PATH:directory
A better way would be to edit your .bash_profile or .profile file (depending on your distribution) to include the above command. That way, it would be done automatically every time you log in.
Most Linux distributions encourage a practice in which each user has a specific directory for the programs he/she personally uses. This directory is called bin and is a subdirectory of your home directory. If you do not already have one, create it with the following command:
$ mkdir bin
Move your script into your new bin directory and you're all set. Now you just have to type:
Note:You can also simply type the script as sequence of commands and save it with some name hello_world
#my first script
echo “Hello World”
Then execute the script with command $bash hello_world