Sunday, 18 August 2013

Getting Started - Linux Shell Scripting Language

What is a Shell..?

    Most of us know the fact that computer understands the language of 0’s and 1’s i.e. Binary language. Whenever we enter any command in the text format, it gets translated to computer-readable form and the processed output which is in binary format gets converted to human-readable form and gets displayed on the screen in the form of texts. Shell performs all these translations. 

    The Linux/Unix shell is a command-line interface which lets its users to interact with the operating system by accepting commands from the users through the keyboard; the shell executes the commands and prints the output on the screen. Unlike the graphical environment (GUI based) that we would normally observe in most of the present computers, the interaction is purely text-based and being command-oriented this kind of interface is called “Command Line interface or CLI”. Before GUI environments were introduced in computer systems, CLI was the only way using which an user could interact and operate the computer system.

Know These Terms

  • Process: Any task performed by the user within the system is a process
  • X-windows or windows: In Linux, the screen can be divided in small portions called windows. It allows users to perform several tasks at a time. Also it becomes easy while switching from one task to another in a nice graphical way.
  • Text terminal: It is the screen through which user can enter commands in text format to instruct the OS to perform a task.
  • Session: Time span between system log in and log off.

Types of Linux/Unix Shell

Bourne Shell

It was developed by Stephen Bourne at Bell Laboratories as an alternative for Thompson Shell and it still remains as a default shell for most of the Unix-like operating systems. Every Unix-like operating system consists at least one shell which is compatible with Bourne Shell. Unlike C and Korn Shells, Bourne Shell doesn’t have complex programming constructs and interactive features. sh is the name of Bourne Shell program and it is situated at /bin/sh.

C Shell

C shell was developed by Bill Joy at University of California as a replacement for the oldest Unix shell- Bourne Shell. csh is the name of C Shell program and % is the shell prompt. It was introduced for those programmers who prefer syntax similar to those used in C language.

Korn Shell

Korn Shell was invented by David Korn at Bell Laboratories as a combined flavor of other Unix shells. It has all the features of C Shell and scripting language very much similar as that of Bourne Shell. It is considered as a member of Bourne Shell family as it uses the shell prompt of Bourne Shell, the $ symbol. Korn shell is the easiest to learn and hence preferred by most of the inexperienced users.

The BASH Shell

BASH is the acronym for Bourne-Again SHell, pun on the name of developer of Bourne Shell - Stephen Bourne and a description of itself as bashing sh, csh and ksh together. It was developed by Brian Fox for GNU Project and as a free software alternative for Bourne Shell. BASH has been distributed all over as a shell for GNU operating system and observed as a default shell on Linux and MacOS X.

Shell Programming- The Basics

  • To have an access to Linux Shell, open the terminal.
  • To see the shell you are having, enter: echo $SHELL
  • In Linux, dollar symbol $ denotes the shell variable.
  • Echo command will display strings on your terminal screen.
  • To link several commands, use pipe (|) operation.
Do not forget to write the shebang- #!/bin/sh at the top of the shell script. It will pass instructions to the shell program /bin/sh.
Linux command should be written in proper syntax for the script to work. Any mistakes will not let the script work.
Shell scripts are nothing but simple text files with .sh extensions.

Writing The First Shell Script- Hello World

  • Open terminal and move to the location where you want to create your script.
  • Write your script in a text file using your favorite text editor and save it with an extension .sh, like
  • Make it executable- chmod 744
  • Run it- sh or ./
Sample Input:

Sample Output:

 That's it for this getting started guide. Stay tuned for some more articles on "Shell Scripting Language".

Also Read:

-By Mandar Shinde