Introduction to Bash Shell in Linux
Bash Stands for “Bourne Again Shell” in the shell ecosystem. It is a free version of Bourne shell distribution associated with the Linux and GNU operating systems. It is similar to the command-line shell with extra features incorporated in it and bash is a specified part of IEEE POSIX. Bash Shell is widely available for various operating systems and a default command-line interpreter for almost all GNU/Linux systems. Bash also includes the features of Cshell and Kshell.
Understanding of Bash Shell in Linux
Let’s see the concept of bash shell in Linux as given below:
- Shell: Shell is a macro processor that allows for an interactive or non-interactive command execution. It is completely based on the graphical user interface so that the user can interact with the underlying operating system.
- Scripting: Scripting means a piece of code which enables us for automatic command execution instead of manual intervention. It basically reduces human effort and cost of implementation.
- Kernel: It is the core of any operating system, which is used to communicate with the hardware and shell for information exchanges
- Applications: These are the graphical user interface application used by the end-users.
Syntax of Bash Shell in Linux
Let see the syntax of the bash shell in Linux along with a list of options that can be used.
bash -option filename
List of Option :
Let us discuss each option in details :
- -c option: When the -c option is used then the command will read from the string. If arguments are after that the string they will assign to the positional parameters beginning with $0.
- -i option: When the -i option is used the shell will be in interactive mode.
- -l option: This option stands for login shell and makes bash to act as if it is invoked for a login shell.
- -r option: When the -r option is used then the shell will be in restricted mode.
- -s option: When the -s option is used or there are no arguments left after option processing then the command will read from the standard input. This feature of option enables the positional parameters to be set when we will invoke an interactive shell.
- -D option: When we use -D option then a list of all double-quoted strings preceded by “$” will be printed on standard output console. These string that is subjected to language translation if the present locale is not C or POSIX. This signifies the -n option, no commands will be executed.
- – – option: A “- -” signifies the end of option and stops further option processing. Any argument passed after the – – will be treated as filename and argument. An argument of – is similar to –.
Now we will understand the concept of Shell Scripting in detail.
What is Shell Scripting?
A shell script is segregation of the list of commands in order to optimize any task and cost-effective, similar to other programming languages with well-defined standards. When you have any experience then it will be beneficial to understand quickly, however, we will make you understand the concept and implementation of shell scripting.
A shell script comprises the following elements:
- Shell Keyword: if, else, break, while, then, etc
- Shell Command: ls, echo, cd, touch, pwd, touch
- Control Flow: if..then..else, case, and shell, for loops, etc.
- Functions: Manually write to perform any operation
Reason for Developing Shell Script
- Used to automate and eliminate repetitive work
- System administrator use it for backing up
- System monitoring is done by developers
- Some new functionality etc.
The steps involved in creating a Shell Script:
We need to create a new file with a vi editor.FileName of script file with extension .sh.
- Step 1: Script will begin with #! /bin/bash
- Step 2: We need to write some code.
- Step 3: Store the script file with name test.sh
- Step 4: In order to execute this script, please type bash then name the file test.sh, starting of ‘#!’ is an operator recognize for a specific line called a “shebang” line which points the script to the interpreter location. So if we use #! /bin/bash, the script will directly recognize and go to the bash-shell.
Sample creation of a small script,
Command ‘ls’ will be executed when we execute the script test.sh, file and display the output.
What is Shell Comments?
Adding any Comment in any program plays a vital role. It means shell script will ignore that command or line.
#This is my first script
Variables are those to store data in terms of characters and numbers. Similarly, shell variables are used to store information at run time provided by any user and they can be used by the shell to evaluate the variable.
For example, below we have created a shell variable and then printed it:
Below is a small script which will use a variable:
#!/bin/bash # Bash Shell
echo "My first Shell Script?"
echo "How do you do $a?"
read data1 ## Reading the variable
echo "I am $b too!"
Advantages and Disadvantages of Bash Shell
Let’s see the advantages and disadvantages of the bash shell:
The syntax and command are approx the same as those directly entered in command line so developer or programmer does not face much problem in implementation. Let see the advantages of the bash shell as given below.
- Writing a shell script is faster and quicker.
- Quick start
- Interactive debugging is available
- Extra care is required to eliminate harmful circumstances.
- Usually, the execution speed is slow.
- There are many flaws with respect to design and implementation.
- It gives very little data structure facilities with respect to other scripting languages like Python.
This is a guide to Bash Shell in Linux. Here we discuss the syntax and concepts of Bash Shell in Linux along with its advantages and disadvantages. You may also have a look at the following articles to learn more –