Linux Shell Scripting – An Introduction

Linux Shell Scripting

Scripts are collections of commands that are stored in a file. The shell can read this file and act on the commands as if they were typed at the keyboard. The shell also provides a variety of useful programming features to make your scripts truly powerful.

Write a simple shell script

Use an editor software to write the script. The following is the simplest “Hello World” program.

# My first script
echo "Hello World!"

and save the above script to the file name “”

We can use any name for saving the script. but here I am using “.sh” extension for my script, since the shell used here is “/bin/sh”. Also it will help us to identify the scripts from a other files and folders.

Explanation for the above program.

The first line of the script is shebang . The #! syntax used in scripts to indicate an interpreter for execution under UNIX / Linux operating systems. In this case, it is /bin/sh. Other scripting languages such as perl, python etc.. can also use this mechanism.

The second line is a comment. Everything that appears after a “#” symbol is ignored by bash. We can use this for documentation, commenting will help us to tourbleshoot the script in future by ourself and by others.

The last line is the echo command. This command simply prints what it is given on the display.

How to execute a shell script

We can execute a shell scrpt using different ways

[tech@nixtree]$ sh

The script doesn’t require execute permission for the above step. All other methods require execute permission. We can set execute permission using the following command.

[tech@nixtree]$ chmod 755

The other ways are;

[tech@nixtree]$ ./

Here we have executed the script after setting the execute permission. This method works only with the scrips in the present working directory.

[tech@nixtree]$ /home/tech/

In this method we use absolute path for the script. This method is irrespective of the present working directory and we can run this script from anywhere in the server.

set PATH variable and execute the script like a command.

The shell maintains a list of directories where executable files (programs) are kept, and when you type in the name of a command, the system just 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.

You can view the list of directories with the following command

[tech@nixtree]$ echo $PATH

In most of the linux distributions /home/USER/bin folder is already present in the PATH list and you just need to create that folder for it to work.

[tech@nixtree]$ mkdir bin
[tech@nixtree]$ mv bin/
[tech@nixtree]$ chmod 755 bin/

Once the above steps are completed, you can execute the script ‘’ like the linux command, no need to worry about the path where it present.

Hello World!

Hope you get a clear idea about the ways the scripts were executed in a linux system. Please add your comments and suggestions here.

Facebook Comments