Linux Shells -
Learn about scripting and the different types of Linux shells.
There are many types of Linux shells. We will discuss a few of them and their features.
You can list down the available shells in your Linux OS. The file /etc/shells contains all
the installed shells on a Linux system. You can list down the available shells in your Linux
OS by typing “cat /etc/shells” in the terminal
1. Bourne Again Shell
Bourne Again Shell (Bash) is the default shell for most Linux distributions. When you
open the terminal, bash is present for you to enter commands. Before bash, some shells
like sh, ksh, and csh had different capabilities. Bash came as an enhanced replacement
for these shells, borrowing capabilities from all of them. This means that it has many of
the features of these old shells and some of its unique abilities. Some of the key features
provided by bash are listed below:
Bash is a widely used shell with scripting capabilities.
It offers a tab completion feature, which means if you are in the middle of
completing a command, you can press the tab key on your keyboard. It will
automatically complete the command based on a possible match or give you
multiple suggestions for completing it.
Bash keeps a history file and logs all of your commands. You can use the up and
down arrow keys to use the previous commands without typing them again. You
can also type history to display all your previous commands.
2. Friendly Interactive Shell
Friendly Interactive Shell (Fish) is also not default in most Linux distributions. As its name
suggests, it focuses more on user-friendliness than other shells. Some of the key
features provided by fish are listed below:
It offers a very simple syntax, which is feasible for beginner users.
Unlike bash, it has auto spell correction for the commands you write.
You can customize the command prompt with some cool themes using fish.
The syntax highlighting feature of fish colors different parts of a command based
on their roles, which can improve the readability of commands. It also helps us to
spot errors with their unique colors.
Fish also provides scripting, tab completion, and command history functionality like
the shells mentioned in this task.
3. Z Shell
Z Shell (Zsh) is not installed by default in most Linux distributions. It is considered a
modern shell that combines the functionalities of some previous shells. Some of the key
features provided by zsh are listed below:
Zsh provides advanced tab completion and is also capable of writing scripts.
Just like fish, it also provides auto spell correction for the commands.
It offers extensive customization that may make it slower than other shells.
It also provides tab completion, command history functionality, and several other
features.
Feature Bash Fish Zsh
Full name Bourne Again Shell Friendly Interactive Z Shell
Shell
Scripting It offers widely It has limited It offers an excellent
compatible scripting scripting features as level of scripting,
with extensive compared to the combining the
documentation other two shells. traditional
available capabilities of Bash
shell with some extra
features.
Tab It has a basic tab It offers advanced Its tab completion
completion completion feature. tab completion by capability can be
giving suggestions extended heavily by
based on your using plugins.
previous commands.
Customization Basic level of It offers some good Advanced
Customization customization customization
through interactive through oh-my-zsh
tools. framework.
User It is less user-friendly, It is the most user- It can be highly user-
Friendliness but being a traditional friendly shell. friendly with proper
and widely used shell, customization.
its users are quite
familiar and
comfortable with it.
Syntax The syntax highlighting The syntax The syntax
highlighting feature is not available highlighting is built-in highlighting can be
in this shell. to this shell. used with this shell
by introducing some
plugins.
Shell Scripting and Components
We first need to create a file using any text editor for the script. The file must be named
with an extension “.sh”, the default extension for bash scripts. The following terminal
shows the script file creation: nano first_script.sh
Every script should start from shebang. Shebang is a combination of some characters
that are added at the beginning of a script, starting with “#!” followed by the name of
the interpreter to use while executing the script. E.g: #!/bin/bash
Variables
A variable stores a value inside it. Suppose you need to use some complex values, like a
URL, a file path, etc., several times in your script. Instead of memorizing and writing
them repeatedly, you can store them in a variable and use the variable name wherever
you need it.
The script below displays a string on the screen: "Hey, what’s your name?” This is done
by echo command. The second line of the script contains the code read name. read is
used to take input from the user, and name is the variable in which the input would be
stored. The last line uses echo to display the welcome line for the user, along with its
name stored in the variable.
# Defining the Interpreter
#!/bin/bash
echo "Hey, what’s your name?"
read nadame
echo "Welcome, $name"
Now, save the script by pressing CTRL+X. Confirm by pressing Y and then ENTER.
To execute the script, we first need to make sure that the script has execution
permissions. To give these permissions to the script, we can type the following command
in our terminal:
chmod +x first_script.sh
Now that the script has execution permissions use ./ before the script name to execute
it.
E.g: ./first_script.sh
Loops
Loop, as the name suggests, is something that is repeating. For example, you have a list
of many friends, and you want to send them the same message. Instead of sending them
individually, you can make a loop in your script, give your friend list to the loop and the
message, and it will send that message to all your friends.
For a general explanation of loops, let’s write a loop that will display all numbers starting
from 1 to 10 on the screen:
# Defining the Interpreter
#!/bin/bash
for i in {1..10};
do
echo $i
done
The first line has the variable i that will iterate from 1 to 10 and execute the below code
every time. do indicates the start of the loop code, and done indicates the end. In
between them, the code we want to execute in the loop is to be written. The for loop will
take each number in the brackets and assign it to the variable i in each iteration. The
echo $i will display this variable’s value every iteration.
Conditional Statements
Conditional statements are an essential part of scripting. They help you execute a
specific code only when a condition is satisfied; otherwise, you can execute another
code. Suppose you want to make a script that shows the user a secret. However, you
want it to be shown to only some users, only to the high-authority user. You will create a
conditional statement that will first ask the user their name, and if that name matches
the high authority user’s name, it will display the secret.
# Defining the Interpreter
#!/bin/bash
echo "Please enter your name first:"
read name
if [ "$name" = "Stewart" ]; then
echo "Welcome Stewart! Here is the secret: THM_Script"
else
echo "Sorry! You are not authorized to access the secret."
Fi
The above script takes the user’s name as input and stores it into a variable (studied in
the Variables section). The conditional statement starts with if and compares the value
of that variable with the string Stewart; if it’s a match, it will display the secret to the
user, or else it will not. The fi is used to end the condition.
Comments
Sometimes, the code can be very lengthy. In this scenario, the code might confuse you
when you look at it after some time or share it with somebody. An easy way to resolve
this problem is to use comments in different parts of the code. A comment is a sentence
that we write in our code just for the sake of our understanding. It is written with a #
sign followed by a space and the sentence you need to write.
E.g: # Defining the Interpreter
The Locker Script
Requirement
A user has a locker in a bank. To secure the locker, we have to have a script in place that
verifies the user before opening it. When executed, the script should ask the user for
their name, company name, and PIN. If the user enters the following details, they should
be allowed to enter, or else they should be denied access.
Username: John
Company name: Tryhackme
PIN: 7385
The Script:
# Defining the Interpreter
#!/bin/bash
# Defining the variables
username=""
companyname=""
pin=""
# Defining the loop
for i in {1..3}; do
# Defining the conditional statements
if [ "$i" -eq 1 ]; then
echo "Enter your Username:"
read username
elif [ "$i" -eq 2 ]; then
echo "Enter your Company name:"
read companyname
else
echo "Enter your PIN:"
read pin
fi
done
# Checking if the user entered the correct details
if [ "$username" = "John" ] && [ "$companyname" = "Tryhackme" ] && [
"$pin" = "7385" ]; then
echo "Authentication Successful. You can now access your locker,
John."
else
echo "Authentication Denied!!"
fi