Nitendra Gautam

Introduction to Bash Script

Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. This blog is the basic introduction to bash scripts.

Common Shell types

Below are the most common shell types that exists today.

  • bash = Bourne again shell
  • sh = shell
  • csh = C shell
  • tcsh = Tenex C shell
  • tclsh = Tcl shell
  • ksh = korn shell
  • ash = a shell
  • bsh = bourne shell
  • zsh = the Z shell

Basic UNIX /Linux directory structure

/ (system root)
 _______________________________|____________________________________
 |    |	    |	 |     |    |	    |	      |	   |	  |    |    |
bin/ boot/ dev/ etc/ home/ lib/ lost+found/ proc/ root/ sbin/ usr/ var/
 |    |     |    |     |    |       |         |    |      |    |    |
 |    |     |    |     |    |       |         |    |      |    |    |-> various
 |    |     |    |     |    |       |         |    |      |    |
 |    |     |    |     |    |       |         |    |      |    |-> Read only files
 |    |     |    |     |    |       |         |    |      |
 |    |     |    |     |    |       |         |    |      |-> Superuser Binaries
 |    |     |    |     |    |       |         |    |
 |    |     |    |     |    |       |         |    |-> User roots home dir
 |    |     |    |     |    |       |         |
 |    |     |    |     |    |       |         |-> system info etc.
 |    |     |    |     |    |       |
 |    |     |    |     |    |       |-> Orphan files (look here after crashes)
 |    |     |    |     |    |
 |    |     |    |     |    |-> System / programming libraries
 |    |     |    |     |
 |    |     |    |     |-> Contains the user accounts home directories
 |    |     |    |
 |    |     |    |-> System configuration files.
 |    |     |
 |    |     |-> Devices
 |    |
 |    |-> The kernel and kernel maps.
 |
 |-> Executeble files. (binaries)


The /usr directory contains a whole lot of things, mainly user accessible things

/usr/local/bin/ and /usr/bin/ It contains binaries

/usr/lib/ and /usr/local/lib/ It contains libraries

/usr/src/linux/ It contains the kernel source related files

Same or similar structure is followed on any operating system that uses directories some of the directory names in Linux/UNIX are UNIX specific.

Exit code in Linux

Exit codes are a number between 0 and 255, which is returned by any Unix command when it returns control to its parent process.

To check the exit code we can use the $? special variable in bash.

Exit CodeMeaning
0successful termination
64base value for error messages
64command line usage error
65data format error
66cannot open input
67addressee unknown
68host name unknown
69service unavailable
70internal software error
71system error (e.g., can’t fork)
72critical OS file missing
73can’t create (user) output file
74input/output error
75temp failure; user is invited to retry
76remote error in protocol
77permission denied
78configuration error

Shell Command Separator /Control Characters

Control characters are code or numbers in a character set that does not represent a written symbol. They are building blocks of Bash scripts which are like metacharacters having a special meaning.

Below are the common seperators and Control characters used in Shell or bash scripts.

Control CharactersDescription
|pipe will take the first commands stdout as the second commands stdin.
||OR if first command is false, it will take the second.
|=OR IS (mostly used in if statements)
&&AND if first command is true, it will execute the second one.
!NOT (mostly used in if and test statements), but as a shell-command it opens a shell to run the command (ex. ! echo foo)
!=NOT IS (mostly used in if statements)
!$last commands last argument
!!repeat last command
=IS (mostly used in if statements)
;will separate 2 commands as if they were written on separate command lines
;;end of a case function in a case statement.
$prefix to a variable like “$customVariable”
$!PID of the last child process.
$$PID of current process (Here PID is Process ID)
$0Shows program that owns the current process.
$1First argument supplied after the program/function on execution.
$2Second argument supplied after the program/function on execution. ($3 etc.)
$#Shows the number of arguments.
$?Any argument (good to use in if statements)
$-current option flags
$_Last argument/Command
$*All arguments
[email protected]All arguments
#commented line, anything on a line after “#” will be overlooked by the script
{start braces (starts a function)
}end braces (ends a function)
[start bracket (multiple-argument specifiers)
]end bracket (multiple-argument specifiers)
@[email protected] is equivalent to “$1” “$2” etc. (all arguments)
*wild card (* can substitute any number of characters)
?wild card (? can substitute any single character)
"quote
'precise quote. (Will even include “‘s in the quote)
.dot will read and execute commands from a file, ( . .bashrc )
&and. as suffix to executed file makes it go to the background(./program &)
0>stdin stream director (I never seen this used in any script)
1>stdout stream director (standard output)
2>stderr stream director (standard error output)
%job character, %1 = fg job 1, %2 = fg job 2, etc.
>>stream director append to a file
<<stdin stream director. (cat > file << EOF ; anything ; EOF)
>stream director that will start at the top of the file (in if statements < and > may be used as greater-then and lesser-then, as: if [ “$1” >= “2” ])
\back-slash, takes away any special meaning with a character
>&stream director to stream director, ie. echo “a” 1>/dev/null 2>&1 this directs 2> to the same place as 1>

Reference

Exit Code Linux