Shell Scripting tutorial provides basic and advanced concepts of Shell Scripting. Shell Scripting is an open-source operating system.
A variable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data. A variable is nothing more than a pointer to the actual data. There are three types of variables in Shell Scripting which are as follows:-
$PATH ————————– displays all directories
$USER ————————– display username
$SHELL ————————– absolute path name of login shell
$TERM ————————– displays types of terminals
$HOME ————————– displays home of the user
$$ —————————— displays process id of current shell
$* —————————— return argument parameter which are quoted together
$@ —————————— return argument parameters which are quoted individually
$? —————————— status (successful=0, unsuccessful=1) of the previous command
$0 —————————— filename of current script
$! —————————— process number/id of the last background command
For example :- a=12, x=”thezenith”, etc.
Creating shell Script
- Create a file with extension .sh
- For Example :- gedit file1.sh
- Inside that file write script for Hello World program as follows :-
- In order to run/execute the file, type ./file1.sh on the terminal.
- Output will be shown on the terminal as:- Hello World
Taking input from User
- In order to take input from user, read keyword is used for taking input from user.
- read variable-name
- Create a file with filename gedit file2.sh
- Write the following code into it :-
echo”Enter the values for two variable : -”
x=`expr$a + expr$b`
y=$(expr”$a” – expr”$b”)
z=$(($a \* $b))
echo”Addition = $x”
echo”Subtraction = $y”
echo”Multiplication = $z”
- Execute file by ./file2.sh
- Output will be:-
Enter the values for two variable
Addition = 5
Subtraction = 1
Multiplication = 6
- Note that all the given ways are valid to perform arithmetic operations. Also, in order to do multiplication operation we need to put backslash (“\”) before *.
Unset the variable
- Create a file with filename gedit file3.sh
- In order to unset a variable we’ll an example as follows :-
- Execute file, file3.sh
- Output will be :-
- Nothing will be printed on the terminal, as we have unset the variable x.