The default shell environment for most GNU/Linux system is Bash ( Bourne Again Shell ).
For any scripting languague in a Linux environment , a script start with a special line called shebang.
Shebang is a line for which #! is prefixed to the environment path. /bin/bash is the interpreter command path for Bash.
Execution of a script can be done in two ways. Either we can run the script as a command-line argument for sh or run a self execution with execution permission. for example:
sh script.sh
If a script is run as command-line argument for sh, the shebang in the script is of no use.
The execution permission for the script can be set as follows:
chmod +x script.sh
For self execution script.
./script.sh
In Bash, each command or command sequence is delimited by using a semicolon or a new line.
cmd1 ; cmd2
or
cmd1
cmd2
Printing in the terminal
echo, printf,
echo $PATH #/usr/local/bin:/usr/sbin:/usr/bin
echo "$PATH" #/user/local/bin:.....
echo '$PATH' #$PATH
Note: when using echo with single quotes, the variable inside the quotes will not be interpreted by Bash, but will be display as is .
echo -e "1\t2\t3"
escaping new line in echo.
printf "%-5s -%-10s %-4s\n" No Name Mark
Playing with variable and environment variable
For every process , environment variables in its runtime can be viewed by:
cat /proc/$PID/environ
Set the PID with the process ID of the relevent process.
for example. assume that an application called gedit is running . We can obtain the progress ID of gedit with pgrep command as follows.
pgrep gedit
1250
cat /proc/1250/environ | tr '\0' '\n'
cat /proc/1250/environ | tr '\0' '\n'
Printing the content of a variable is done using by prefixing $ with the variable name as follows:
var="hello" #Assignment of value to variable var.
echo $var
or
echo ${var}
There some default system environment variable:
SHELL, USER, HOME, PATH, UID and so on.
for example:
echo $SHELL # /bin/bash
Finding length of string
Get the length of a variable value as follow:
length=${#var}
for example:
var=12345
echo ${#var} # 5
Check for super user
if [ $UID -ne 0 ]; then
echo "Non root user. please run as root."
else
echo "Root user"
fi
Doing math calculations with the shell
The bash shell environment can perform basic arithmatic operations using the commands let, ( ( ) ) and [ ]. The two utilities expr and bc are very helpful performing advanced operations.
While using let, we use variable names without the $ prefix, for example:
n1=2;
n2=3;
let result=n1+n2;
let result++;
echo $result;
The [ ] operator can be used similar to the let command as follows:
result=$[ n1 + n2 ]; # No space between = and $
(( )) can be alse used.
result=$(( n1 + n2 ));
expr can be alse used for basic operation.
result=`expr 3 + 4`
result=$(expr $n1 + 5);
All of above methods do not support floating point numbers, and operate on integer only.
bc the precision calculator is an advanced utility for mathematical operations. It has a wide range of options.
echo "4 * 0.56" | bc
result=`echo "$n1 * 1.5" | bc`
Arrays
array_var=(1 2 3 4)
array_var[0]="test1"
array_var[1]="test2"
array_var[2]="test3"
varay_var[3]="test4"
echo ${array_var[0]};
#echo $array_var[0]; # the usage is error.
echo ${array_var[@]}; # print array length or ${array_var
}
Function and Arguments
A function can be defined as follows:
function fname(){
statements;
}
or
fname(){
statements;
}
$1 is the first argument
$n is the n argument
"$@“ expands as "$1" "$2" "$3" and so on
"$*" expands as "$1c$2c$3", where c is the first character of IFS
”$@" is the most use one.
Store command output
cmd_out=$(COMMAND)
or
cmd_out=`ls | cat -n `
for example:
cmd_out=$(ls | cat -n)
cmd_out=`ls | cat -n`
Spawning a separate process with subshell
Subshell are separate processes. A subshell can be defined using the ( ) operator as follows: