Updated June 23, 2023
Introduction to Bash Shell in Linux
Bash Stands for “Bourne Again Shell” in the shell ecosystem. It is a free version of the Bourne shell distribution associated with the Linux and GNU operating systems. It is similar to the command-line shell with extra features incorporated, and bash is a specified part of IEEE POSIX. Bash Shell is widely available for operating systems and is 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 allowing 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 that enables automatic command execution instead of manual intervention. It reduces human effort and the 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’s see the syntax of the bash shell in Linux and a list of options that can be used.
bash -option filename
List of Options:
- -c
- -i
- -l
- -r
- -s
- -D
- —
Let us discuss each option in detail:
- -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 act as if it is invoked for a login shell.
- -r option: When the -r option is used, the shell will be restricted.
- -s option: When the -s option is used, or there are no arguments left after option processing, the command will read from the standard input. This optional feature sets the positional parameters when we invoke an interactive shell.
- -D option: Using the D option prints a list of all double-quoted strings preceded by “$” on the standard output console. These strings are 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 the option and stops further option processing. The system treats any argument passed after the – – as a 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 segregates the list of commands to optimize any task and is cost-effective, similar to other programming languages with well-defined standards. When you have any experience, 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 administrators use it for backing up
- Developers do system monitoring
- 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 a script file with extension .sh.
- Step 1: The 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: To execute this script, please type bash, then name the file test.sh, starting with ‘#!’ is an operator recognized 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,
Code:
#!/bin/bash
ls
When we run the script test.sh, it executes the ‘ls’ command, displays the file, and shows the output.
Output:
What are Shell Comments?
Adding any Comment in any program plays a vital role. It means the shell script will ignore that command or line.
Example:
#This is my first script
Shell Variables
Variables are those to store data in terms of characters and numbers. Similarly, shell variables store information a user provides at runtime, allowing the shell to evaluate the variable.
For example, below, we have created a shell variable and then printed it:
Code:
variable ="Hey"
echo $variable
Below is a small script that will use a variable:
#!/bin/bash # Bash Shell
a="fine"
b="Good"
echo "My first Shell Script?"
echo "How do you do $a?"
read data1 ## Reading the variable
echo "I am $b too!"
Output:
Advantages and Disadvantages of Bash Shell
Let’s see the advantages and disadvantages of the bash shell:
Advantages
The syntax and command are approximately the same as those directly entered in the command line, so the developer or programmer does not face many problems in implementation. Let’s see the advantages of the bash shell as given below.
- Writing a shell script is faster and quicker.
- Quick start
- Interactive debugging is available
Disadvantages
- You must take extra care to eliminate harmful circumstances.
- Usually, the execution speed is slow.
- There are many flaws concerning design and implementation.
- It gives very few data structure facilities concerning other scripting languages like Python.
Recommended Articles
This is a guide to Bash Shell in Linux. Here we discuss the syntax and concepts of Bash Shell in Linux and its advantages and disadvantages. You may also have a look at the following articles to learn more –