Friday, 7 February 2014

Linux Shell Basic – bash shell (note 1)

For most linux system, the default shell is bash, an acronym for Bourne Again Shell.  bash is an enhanced replacement for sh, the original Unix shell program written by Steve Bourne.

The shell is a program that takes your commands from the keyboard and gives them to the operating system to perform.

The bash shell is located in /bin directory.  In /bin directory, you will see a file named “bash”, this is the binary program for bash shell. 

Shell Operations

A shell is a program that interprets commands and allows a user to execute commands by typing them manually at a terminal. A shell is not an operating system. It is a way to interface with the operating system and run commands.

bash shell is just like DOS command prompt.  It has some built-in understanding of “shell commands” and “rules”.  shell script files are like the .bat files in DOS, it contains basic programming logic and utility commands for the shell to interpret and execute.

The following is a brief description of the shell’s operation when it reads and executes a command.  Basically, the shell does the following:

  1. Reads its input from a file or interactive console.
  2. Breaks the input into words and operators or refer to as tokens. 
  3. Parses the input tokens into simple and compound commands.
  4. Breaking the expanded tokens into lists of filenames and commands and arguments.
  5. Performs any necessary redirections and removes the redirection operators and their operands from the argument list.
  6. Executes the command.
  7. Waits for the command to complete and collects its exit status.

For more details on bash shell, you can refer to http://www.gnu.org/software/bash/manual/bashref.html

How is BASH different form the DOS command prompt?

  1. Case Sensitivity:
    In Linus, commands and filenames are case sensitive. 
  2. “\” vs “/”:
    in DOS “/” is use for command argument delimiter and “\” is a directory separator.  In Linux, the “/” is the directory separator and “\” is an escape character (see special characters for BASH on the use of “\”).
  3. Filenames:
    The DOS uses “eight . three” filename convention.  In Linux, there is no file extension.  Periods can be placed at any part of the filename.

Special Characters for BASH

  • \
    Escape character.  If you want to reference a special character, you must “escape” it with a backslash first before you can put in the special character for BASH to understand.
  • /
    Directory separator.
  • .
    Current directory.  Or “hided” files when it is the first character in a filename.
  • ..
    Parent directory.
  • ~
    User’s home directory.
  • *
    Represents 0 or more characters in a filename, or by itself, represents all files in a directory.
  • ?
    Represents a single character in a filename.
  • [ ]
    Can be used to represent a range of values. e.g [0-9] [A-Z]
  • |
    ”Pipe”.  Redirect the output of one command into another command.
  • >
    Redirect output of a command into a new file.
  • >>
    Redirect output of a command onto the end of an existing file.  Or append.
  • <
    Redirect the output of a command into a program.
  • ;
    Command separator.  Allows you to execute multiple commands on a single line.
  • &&
    Command separator as “;”, but only runs the second command if the first one finished without errors.
  • &
    Execute a command in the background and immediately get your shell back.

Executing Commands

  • Most common/utility commands are located in your shell’s “PATH”, meaning that you can just type the name of the program to execute it and the shell will search for the program in the “PATH” and do the execution.   
    You can run the following command to see your current shell’s “PATH”.
    # echo $PATH
  • To execute commands that are not in your PATH, you have to give the complete locations of the command.
    /home/john/myprogram
    ./myprogram (Execute a program in the current directory)
    ~/myprogram (Execute a program from a personal directory)

Do note that you need “./” before you run any program that is not in your “PATH”

3 comments:

  1. Thank you so much admin for providing the great information and i am learning some real stuff about the Linux and also about the CISCO which are most important for IT profession and thanks for posting these blogs.
    Knowledgeadventure.com Games

    ReplyDelete
  2. I read the post but the most important point which i find is How is BASH different form the DOS command prompt.
    The three points you made are really good and helpful.
    seo services islamabad

    ReplyDelete
  3. hello admin how are you doing?
    I read your post about Linux Shell Basic – bash shell (note 1).
    Your communication style is so generous that you present the information in just a simple way.
    gomovies

    ReplyDelete