Saturday, December 20, 2014

Unix Bash Shell and Executing Commands

Shell is a program that interprets commands. Shell is not an operating System(OS) but a way to interface with operating system to execute commands. It allows user to execute commands manually from terminal.

 Fig 3.1 Bash Shell Prompt

What is Bourne Again SHell(BASH)?

Bash is free replacement shell to Bourne shell which was originally written by Steve Bourne. Since it is free shell hence it has been adopted as default shell on most of the Unix/Linux systems.

How Bash is Different From DOS Command Prompt?


  • Case Sensitive:  In Unix/Linux, commands and filenames are case sensitive i.e. 'EXIT' and 'exit' are different unlike DOS where either of the two will work as command but in Linux 'EXIT' will throw an error on execution.
  • "\" and "/":  In Unix/Linux '/' is directory separator which is '\' in DOS.
  • Filename: DOS follows "EIGHT DOT THREE" filename convention that means 8 character long filename, then dot(.) followed by 3 charaters' file extension e.g. FILEMAME.TXT. In Unix/Linux there is no file extension. Periods can be placed as a part of filename.

Special Characters



Before learning commands it is important to know about some special characters that shell interprets in different ways.

'\' : If you want to reference special character you need to "ESCAPE" it using back-slash('\'). Example: Filename\* (For it to be interpreted by shell as Filename*).

'/': Used to separate directory hierarchy. /home/user1/data

'.':  Dot. Indicates current directory. Can also hide a file if it is the first character in a filename.

'..': Dot Dot. Indicates parent directory.

'~': Tilde. User's home directory.

* : Represents 0 or more characters in the filename. file* can represent file, file1, filexyz, file100 etc.

? : Represents exact single character. File?.txt can represent File1.txt, Filex.txt but not File.txt or File22.txt.

[ ]: Can be used to represent a range of values, e.g 0-9, A-Z, etc. Example: hello[0-2].txt represents hello1.txt, hello1.txt and hello2.txt

| : Pipe. Redirects output of one command to another. ls|cat

>: Redirects output of a command to a file. If a file already exist then overwrite it.
Example: ls > file1.txt

>>: Redirects output of a command to end of file.

; : Semi colon. Command separator.

&& : Command separator but second command will run only if first command runs successfully.
Example: cd /var/logs && cat log1.txt

&: Execute command in background. Output does not flash on screen. Example: ls &

Executing Commands 

Most of commands that we run, are located in Shell's "PATH". You just type the name of program rather giving full path of the program. "PATH" variable in your shell helps to run command without giving full path. This variable includes most common program locations. Location are separated 
by :(colon).

Value of PATH variable can be checked by executing following command.

$PATH

Fig 3.2
If you want to run a command/program that is not included in PATH variable you will have to give full path to the program to execute it.

Command Syntax


Commands can be run by giving command name only or arguments can be passed along command.

Example: command [-argument1] [-argument2][-argument3argument4] [--argument5] [Filename]

$ls
$ls -l -r -t
$ls -lrt
$ls -lrt --color
$cat -n filename

Note: '$' is prompt in above examples.

Getting Help

When you are struck or need help about any Linux command, you are just some keystrokes away!!

built-in help

Many commands have built in help screens that can be invoked using -h or --help flags along with command.

Fig 3.3

Online Manual Pages

Many commands come with manual pages with them. You can explore using 'man' command followed by command name.

Fig 3.4 Man Page
Sometimes you might not remember the name of the command you want to search about, then you can use -k option of man command. It searches the supplied "word" in argument in DESCRIPTION part of man page.

For example, man -k permission will display man pages having "permission" word in its DESCRIPTION field.

Fig 3.5


In our next topic we shall cover basic commands and will explore Linux Filesystem. Till then keep LearnLinuxWithEase and have nice time. Smile :)

Your comments and suggestion are most welcome.

No comments:

Post a Comment