New Document
How to write shell script

Following steps are required to write shell script

  • (1) Use any editor like vi or mcedit to write shell script.
  • (2) After writing shell script set execute permission for your script as follows
  • chmod permission your-script-name
    $ chmod +x your-script-name
    $ chmod 755 your-script-name
  • (3) Execute your script as
  • chmod permission your-script-name
    bash your-script-name
    sh your-script-name
    $ bash bar
    $ sh bar
    $ ./bar
  • In the last syntax ./ means current directory, But only . (dot) means execute given command file in current shell without starting the new copy of shell, The syntax for . (dot) command is as follows
  • chmod permission your-script-name
    . command-name
    $ . foo
  • Now you are ready to write first shell script that will print "Knowledge is Power" on screen.
  • chmod permission your-script-name
    $ vi first
    # My first shell script
    echo "Knowledge is Power"
  • After saving the above script, you can run the script as follows:
  • chmod permission your-script-name
    $ ./first
  • This will not run script since we have not set execute permission for our script first; to do this type command
  • chmod permission your-script-name
    $ chmod 755 first
    $ ./first
  • First screen will be clear, then Knowledge is Power is printed on screen.
Shell script command:
Script Command(s)Meaning
$ vi firstStart vi editor
# # My first shell script #
# followed by any text is considered as comment. Comment gives more information about script, logical explanation about shell script. Syntax: # comment-text
clear clear the screen
echo "Knowledge is Power" To print message or value of variables on screen, we use echo command, general form of echo command is as follows syntax: echo "Message"

1)Write following shell script, save it, execute it and note down the it's output.

$ vi ginfo
# Script to print user information who currently login , current date & time
echo "Hello $USER"
echo "Today is \c ";date
echo "Number of user login : \c" ; who | wc -l
echo "Calendar"
exit 0

You can customize your command prompt using environment variable PS1 explained in Environment tutorial.

Shell Types:

In UNIX there are two major types of shells:

  1. The Bourne shell. If you are using a Bourne-type shell, the default prompt is the $ character.

  2. The C shell. If you are using a C-type shell, the default prompt is the % character.

There are again various subcategories for Bourne Shell which are listed as follows:

  • Bourne shell ( sh)

  • Korn shell ( ksh)

  • Bourne Again shell ( bash)

  • POSIX shell ( sh)

The different C-type shells follow:

  • C shell ( csh)

  • TENEX/TOPS C shell ( tcsh)

The original UNIX shell was written in the mid-1970s by Stephen R. Bourne while he was at AT&T Bell Labs in New Jersey.

The Bourne shell was the first shell to appear on UNIX systems, thus it is referred to as "the shell".

The Bourne shell is usually installed as /bin/sh on most versions of UNIX. For this reason, it is the shell of choice for writing scripts to use on several different versions of UNIX.

In this tutorial, we are going to cover most of the Shell concepts based on Borne Shell.

Shell Scripts:

The basic concept of a shell script is a list of commands, which are listed in the order of execution. A good shell script will have comments, preceded by a pound sign, #, describing the steps.

There are conditional tests, such as value A is greater than value B, loops allowing us to go through massive amounts of data, files to read and store data, and variables to read and store data, and the script may include functions.

Shell scripts and functions are both interpreted. This means they are not compiled.

We are going to write a many scripts in the next several tutorials. This would be a simple text file in which we would put our all the commands and several other required constructs that tell the shell environment what to do and when to do it.

Example Script:

Assume we create a script. Note all the scripts would have .sh extension. Before you add anything else to your script, you need to alert the system that a shell script is being started. This is done using the shebang construct. For example:


This tells the system that the commands that follow are to be executed by the Bourne shell. It's called a shebang because the # symbol is called a hash, and the ! symbol is called a bang.

To create a script containing these commands, you put the shebang line first and then add the commands:

Shell Comments:

You can put your comments in your script as follows:


# Script follows here:

Now you save the above content and make this script executable as follows:

$chmod +x

Now you have your shell script ready to be executed as follows:


This would produce following result:

index.htm  unix-basic_utilities.htm  unix-directories.htm    unix-communication.htm    unix-environment.htm

Note: To execute your any program available in current directory you would execute using ./program_name

Extended Shell Scripts:

Shell scripts have several required constructs that tell the shell environment what to do and when to do it. Of course, most scripts are more complex than above one.

The shell is, after all, a real programming language, complete with variables, control structures, and so forth. No matter how complicated a script gets, however, it is still just a list of commands executed sequentially.

Following script use the read command which takes the input from the keyboard and assigns it as the value of the variable PERSON and finally prints it on STDOUT.

# Script follows here:
echo "What is your name?"
echo "Hello, $PERSON"

Here is sample run of the script:

What is your name?
Zara Ali
Hello, Zara Ali
Previous                                                                                                                                                       Next

Back to Top