Friday, 10 May 2013

How to find current Shell in Linux?

A very simple question that most of the Linux beginners have. Which shell am i using? How can i change it?When you write any shell script you need to specify which shell you want to use to execute your script(Given as an argument to Shebang).

    Be default on most of the distros the shell you have is BASH(Bourne-Again SHell). You can have other shells like CSH(C SHell), KSH (Korn SHell) etc.

Default BASH is as follows -


How to install other Shells?

  Let us first see how can we install other shells, change from BASH(default) to new shell and back.Lets say you want to install KSH (Korn SHell) . Simply type ksh in your console. If it is installed you will directly see $ symbol instead of your normal aniket@aniket-Compaq-610:~$  representation.If you do not get such a change you will see program not installed. So now you need to install it.
     Type sudo apt-get install ksh . Your ksh will now be installed. Again type in ksh which will bring you to korn shell with a $ symbol.To return back to your BASH shell simple type exit and enter.

KSH (Korn SHell) looks like below -

Now lets get to our main question. How do we figure out what shell are we using.

What Shell I am using?

There are  3 Ways in which users generally check their Shell. Let me explain each of them.Note all the snapshots here after will be executed in KSH (Korn SHell) so ksh must be the answer we are interested in.This is just FYI but point is to find what shell are we using.

  1. Just type echo $SHELL in your console(Not recommended method).

    What this will give is your default Shell not your current shell. So in both BASH as well as KSH you will get output as /bin/bashScreen shot for the same is -


  2. Type in echo $0 in your console(Simplest)

    $0 gives you name of the Shell or Shell script you are using. Screen shot -
  3. Type ps -p $$ in your console(the smart way)

    $$ symbol gives you the PID of the process running your current Shell. PS command gives you the PID of various running process(Try ps -ax to see yourself). -p argument take the specific PID you wish to see. So ps -p $$ whill give you your Current Shell with PID.
    Screen shot -
  4.  
     Play around with different Shells. Each have their own flavor. 

What is Shebang or Hashbang in Unix/Linux?

In a script if the first line consists of characters number sign and exclamation sign (i.e #!)  then such a sequence is know as Shebang or a Hashbang.

 This hashbang takes arguments. The first argument is always the path to the interpreter that will be used to interpret the script code  to follow.

     Suppose you are writing a shell script then the 1st line of your script would be something like #! /bin/sh . Code which will follow this will be interpreted by your shell(whatever you have usually this is Bourne shell).

   Another point to note that hashbang begins with a # character which is interpreted as comment in most of the scripts. So the corresponding interpreter will ignore this line.

Syntax

   Syntax is very simple
   #! interprter [optional arg]
Note this must be the 1st line of your script.

   The interpreter must usually be an absolute path to a  program that should be used to interpret rest of the script code.

Example

Some usage examples are - 
  • #!/bin/sh — Execute the file using sh, the Bourne shell, or a compatible shell
  • #!/bin/csh -f — Execute the file using csh, the C shell, or a compatible shell, and suppress the execution of the user’s .cshrc file on startup
  • #!/usr/bin/perl -T — Execute using Perl with the option for taint checks
  • #!/usr/bin/php — Execute the file using the PHP command line interpreter
  • #!/usr/bin/python -O — Execute using Python with optimizations to code
  • #!/usr/bin/ruby — Execute using Ruby

Purpose of a Hashbang

    Purpose is fairly straight forward. Lets say you have a perl script(GetIP.pl) and perl module is installed at /usr/bin/install/perl . Every time you wish to execute this file(from any directory you are in) you will need to give the absolute path where perl module is located to run the script /usr/bin/install/perl GetIP.pl  but using hashbang all you need to do is  GetIP.pl . It will execute the script using perl module directly.

What happens behind the scene(Magic number)?

The Shebang is actually a human readable instance of magic number in executable file. The magic byte string being 0x23 0x21 , the two character encoding in ASCII. The magic number is detected by "exec" family of functions which determine whether the image file is a script or an executable binary. The presence of  shebang will result in execution of specific executable, usually an interpreter for the script's language.
t> UA-39527780-1 back to top