Bash stands for Bourne Again Shell. A Bash Shell Script is a plain text file containing a set of various commands that we usually type in the command line. It is used to automate repetitive tasks on Linux. To automate day to day automation task, system admins write bash script in Linux system. Bash script has .sh extension but the extension is not mandatory.
A Shell is basically a command-line interpreter between user and kernel or a complete environment specially designed to run commands, shell scripts, and programs.
- Easy to use
- Time saving
- Automated
- Can be installed on all Linux system
- Portable
- Can run multiple commands
- There may be errors in shell scripting that prove to be quite costly.
- The programs in shell script are quite slow while executing and a new process is required for every shell command executed.
- Different platforms in shell scripting may also have compatibility problems.
Bash script starts with #! referred as the shebang followed by /bin/bash it actually tells the path of the interpreter to execute the commands in the script.
echo: echo is a built-in command in Bash, which is used to display the standard output by passing the arguments. It is the most widely used command for printing the lines of text/String to the screen.
bash script_name.sh
OR
./script_name.sh
#!/bin/bash
echo "******************"
echo "Hello World!"
echo "******************"
Variables are used to store information.
Variable-name="Variable-value"
echo $variable-name
Set variable user="john"
echo $user ---> john
Print environment variables
echo "This is user ${user} in the team" -----> This is user john in the team
Use back tick to print output of the command as shown below
echo "There are `wc -l > hello.txt` lines in hello.txt file" -----> There are 5 lines in hello.txt file
-
$? ---- Exit status of last run command, 0 means success and non-zero indicates failure.
-
$0 ---- File name of our script
-
$1..$n ---- Script arguements
-
$# ----- number of args that our script was run with
-
$? ---- Exit status of last run command, 0 means success and anything else indicates failure.
ls -l
$? --> 0 (meaning command was succesful)
lsss -l
$? ---> Non-zero value (meaning commnad was not succesful or wrong command)
- $0 ---- File name of our script
- $1..$n ---- Script arguements
#!/bin/bash
echo "Script name is $0"
echo "First argument passed is $1"
echo "Second argument passed is $2"
./hello.sh Hello World
Script name is hello.sh
First argument passed is Hello
Second argument passed is World
echo "The user is $USER" --> The user is John
echo 'The user is $USER' --> The user is $USED
In every user home directory there is a hidden file called .bashrc. If you place export variable and value this file. It will become permanent export variable. We often use it for setting envirnment variables like JAVA_HOME, MAVEN_HOME. If you want to add export variable globally for all user then edit etc/profile and add export variable.
ls -a
vi .bashrc (edit this file and update export variable)
export JAVA_HOME="usr/bin/jvm"
-p [will stay to take input] -sp [ will make password invisible when user enter]
#!/bin/bash
echo "Enter your Name:"
read Name
echo "Enter username and password"
read -p 'username: ' username
read -sp 'password: ' psw
If [condition]
then
something
elif
something
else
something
fi
-
==
-
!=
-
=
-
<=
-
'>'
-
'<'
-
&&
-
||
-
!
-
b operator: Checks whether a file is a block special file or not.
-
c operator: Checks whether a file is a character special file or not.
-
d operator: This operator checks if the given directory exists or not.
-
e operator: This operator checks whether the given file exists or not.
-
r operator: This operator checks whether the given file has read access or not.
-
w operator: This operator check whether the given file has write access or not.
-
x operator: This operator check whether the given file has execute access or not.
-
s operator: This operator checks the size of the given file.
for variable in <list>
do
something
done
while [condition]
do
something
done
Find shell in the terminal
echo $shell