Home » Unix command and scripts » What is shell and Shell Scripts

What is shell and Shell Scripts

learn unix shell scripting

What is Shell

Whenever you login to a Unix system you are placed in a program called the shell. The shell acts as a command interpreter; it takes each command and passes it to the operating system kernel to be acted upon. It then displays the results of this operation on completion on your screen.

There are different Flavor of shell in Unix

Finding out which shell you are using  Information about which shell you are using is held in the SHELL environment variable. The command echo $SHELL displays the value of this variable.This displays the default shell of the system. You can change the shell though in the script you execute.You can identify which shell you are presently using from the last part of the pathname.

ShellDescription
Bourne shellThis is the original Unix shell written by Steve Bourne of Bell Labs. It is available on all UNIX systems.
Below is written  in the beginning of shell scripts to specify the shell to use for the scripts. The default prompt on the Unix for this is $
#!/bin/bsh
C shellThis shell was written at the University of California, Berkeley. It provides a C-like language with which to write shell scripts – hence its name.The default prompt on the Unix for this is %
Below is written  in the beginning of shell scripts to specify the shell to use for the scripts
#!/bin/csh
Korn shellThis shell was written by David Korn of Bell labs. It is now provided as the standard shell on Unix systems. It provides all the features of the C shell with a shell programming language similar to that of the original Bourne shell. It is the most efficient shell. Consider using this as your standard interactive shell.The default prompt on the Unix for this is $Below  is written in the beginning of shell scripts to specify the shell to use for the scripts
#!/bin/ksh

What is shell Scripts

A shell script is a collection of command which are executed in a order given. There are conditional statement and looping also available like if ,while which helps in finding if a particular value is greater than another value .
To write any comments in the shell scripts ,it has to be written with # preceded
Example
# Author of the script is

See also  Compress And Uncompress in Linux

There are some variables which are set internally by the shell and which are available to the user. They are given below in the table.

NameDescription
$1 – $9these variables are the positional parameters.
$0the name of the command currently being executed.
$#the number of positional arguments given to this invocation of the shell.
$?the exit status of the last command executed is given as a decimal string. When a command completes successfully, it returns the exit status of 0 (zero), otherwise it returns a non-zero exit status.
$$the process number of this shell – useful for including in
filenames, to make them unique.
$!the process id of the last command run in the background.
$*a string containing all the arguments to the shell, starting at $1.

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

Commands in Shell

  • All shells have a number of built-in commands which are executed in the shell’s own process like echo ,cd .
  • When you enter a command, the shell checks to see, if the command is a shell built-in such as echo or cd ,it is directly interpreted by the shell.
  • if the command begins with a / shell assumes that the command is the absolute path name of an executable error occurs if the executable is not found.
  • if not built-in and not a full pathname shell searches the directories in the PATH from left to right for the executable.Current working directory may not be in PATH
  • If PATH is empty or is not set, only the current working directory is searched for the executable
See also  PuTTY CONNECTION MANAGER (PuttyCM)

Unix commands are executable binary files located in directories with the name bin (for binary). Many of the commands that you use are located in the directory /usr/bin.

A typical value for the PATH variable might be:


/bin:/usr/bin:/usr/local/utils/bin:$HOME/bin

Example of Shell Scripts

cat test.sh

#Written by Techgoeasy
TODAY=date '+%m%d%y_%H%M'
echo "This is techgoeasy at" $TODAY

How to write the shell scripts

  • Scripts must contain complete documentation
  • Each script must contain internal comments that document
  • The name of the script
  • The purpose for the script
  • The name of the author(s)
  • The date the script was written
  • The date(s) the script was modified.
  • The name of each script must reflect its use
  • Each script must have a suffix that describes the script, for example “.sh” for Bourne shell scripts, “.ksh” for Korn shell or “.cgi” for Common Gateway Interface scripts.


Lets create the first shell script

 #!/bin/ksh   # This script displays the date, time, username and
# current directory.
echo "Current date and time is:"
date
echo
echo "Your username is: `whoami` \\n"
echo "Your current directory is: \\c"
pwd echo "Your system name is :`hostname`\\n"    

Explanation of each line in the scripts is given below

1. #!/bin/ksh as the first line of the script .It is used to tell the script to execute with ksh shell

2. The second two lines beginning with a hash (#) are comments and are not interpreted by the shell.

3.Use comments to document your shell script.

4. The backquotes (`) around the command whoami illustrate the use of command substitution.The command whoami will be executed and output will be printed at that position. Whoami tells the user information

See also  What is crontab in Linux

5. The \\n is an option of the echo command that tells the shell to add an extra carriage return at the end of the line.

6.The \\c tells the shell to stay on the same line.

7.  hostname command gives the hostname of the system

How to execute the Shell Scripts

Once the shell scripts are created , they can be executed using two ways
(1) We specify the type of shell and then the script name

sh test.sh

(2) We change the permission of the shell scripts to perform execution

chmod +x test.sh
./test.sh

Other Useful stuff

We can have if-then statement , looping , functions in the shell scripts also.Please go through below links to check on it
shell script if then else
loop in shell script
function in shell script

External Reference
https://en.wikipedia.org/wiki/Shell_script

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top