Bash SHELL Script in Linux
Bash SHELL Script in Linux
#!/bin/bash
echo "Hello World"
Run Command
You can run bash file by two ways. One way is by using bash command and another
is by setting execute permission to bash file and run the file. Both ways are shown
here:
$ bash First.sh
or,
$ chmod 755 file.sh
$ chmod a+x First.sh
$ ./First.sh
Echo command
You can use echo command with various options. Some useful options are
mentioned in the following example. When you use ‘echo’ command without any
option then a newline is added by default. ‘-n’ option is used to print any text without
new line and ‘-e’ option is used to remove backslash characters from the output.
Create a new bash file with a name, ‘echo_example.sh’ and add the following script.
#!/bin/bash
echo "Printing text with newline"
echo -n "Printing text without newline"
echo -e "\nRemoving \t backslash \t characters\n"
Run the file with bash command.
Comment in bash
‘#’ symbol is used to add single line comment in bash script. Create a new file named
‘comment_example.sh’ and add the following script with single line comment.
#!/bin/bash
When running the file, the line with hash will not be printed.
While Loop
Create a bash file with the name, ‘while_example.sh’, to know the use
of while loop. In the example, while loop will iterate for 5 times. The value
of count variable will increment by 1 in each step. When the value of count variable
will 5 then the while loop will terminate.
#!/bin/bash
valid=true
count=1
while [ $valid ]
do
echo $count
if [ $count -eq 5 ];
then
break
fi
((count++))
done
Result:
$ bash while_example.sh
For – Loop
The basic for loop declaration is shown in the following example. Create a file named
‘for_example.sh’ and add the following script using for loop. Here, for loop will
iterate for 10 times and print all values of the variable, counter in single line.
Example script:
#!/bin/bash
for (( counter=10; counter>0; counter-- ))
do
echo -n "$counter "
done
printf "\n"
Result:
$ bash for_example.sh
Result:
$ bash user_input.sh
IF statement
You can use if condition with single or multiple conditions. Starting and ending block
of this statement is define by ‘if’ and ‘fi’. Create a file named ‘simple_if.sh’ with the
following script to know the use if statement in bash. Here, 10 is assigned to the
variable, n. if the value of $n is less than 10 then the output will be “It is a one digit
number”, otherwise the output will be “It is a two digit number”. For comparison, ‘-
lt’ is used here. For comparison, you can also use ‘-eq’ for equality, ‘-ne’ for not
equality, ‘-gt’ for greater than, and ‘-lt’ for less than in bash script.
example script:
#!/bin/bash
n=10
if [ $n -lt 10 ];
then
echo "It is a one digit number"
else
echo "It is a two digit number"
fi
#!/bin/bash
OR condition in IF statement
‘||’ is used to define OR logic in if condition. Create a file named ‘if_with_OR.sh’ with
the following code to check the use of OR logic of if statement. Here, the value
of n will be taken from the user. If the value is equal to 15 or 45 then the output will
be “You won the game”, otherwise the output will be “You lost the game”.
#!/bin/bash
if [[ ( $n -eq 15 || $n -eq 45 ) ]]
then
echo "You won the game"
else
echo "You lost the game"
fi
ELSE IF statement
The use of else if condition is little different in bash than other programming
language. ‘elif’ is used to define else if condition in bash. Create a file named,
‘elseif_example.sh’ and add the following script to check how else if is defined in
bash script.
!/bin/bash
if [ $n -eq 101 ];
then
echo "You got 1st prize"
elif [ $n -eq 510 ];
then
echo "You got 2nd prize"
elif [ $n -eq 999 ];
then
echo "You got 3rd prize"
else
echo "Sorry, try for the next time"
fi
!/bin/bash
function hello() {
echo “Hello world”
}
when run, it will print the text/data declared inside the function
Note:
Using parameter -> allowing user to add parameter to variable
Example parameter:
Name=$1
Name=$@ (multiple parameter values)
Environment variable -> help to know the system environment, establish before our
script run
List of environment variables:
$PATH ->file path/directory
$TERM -> Terminal
$EDITOR -> Editor
$HOME -> user’s home directory
$HOSTNAME -> name of the computer
$SHELL -> shell which is being use
$USER