Friday, August 27, 2010

The inside picture of a command execution in shell

Shell is the basic scripting tool which you will encounter in any Unix system. Writing those handy little scripts are probably the first time when you got the 
feel of an 'advanced user'. Any real programmer will vouch for that.

But to be duly noted is the question, "What actually happens when you drop a command in the shell?" Here, is the basic idea.

1. The shell checks whether its a built-in shell command. If so, executes it 
    directly.
    e.g. cd, echo, ...
2. Now it checks whether the command is aliased. If the alias is obtained 
    to be a built-in shell command, it goes back to step 1. If its an executable
    binary, voila!
3. Next the shell search in the PATH environment variable, and nowhere 
    else, for executable binaries with this particular name. If one is found, 
    thats it!   
4. If you are here, i must say sorry on behalf of the shell because no such 
    command could be found in the system.

Tips:
*      echo $PATH 
    will display the current PATH. You can add a custom path by using
        PATH=$PATH: 
*  If your custom path is just '.', it means the present working directory 
   will be added to the PATH, always. Little tricky, but its worth it.

N.B.
!!  Editing the PATH one time has effect only till you close the particular 
    terminal.
    If you want a permanent change, you have to edit, using any text 
    editor, a file called '.bashrc', present in your home folder. Or you can 
    find it as, 
        locate .bashrc

No comments:

Post a Comment