Linux Shells

Introduction

ChatGPT said:

Most people use the Graphical User Interface (GUI) to do tasks with clicks, but nearly everything can also be done using the Command-Line Interface (CLI)—by typing commands.

Think of it like a restaurant:

  • GUI is like ordering from a menu—quick and easy.

  • CLI is like going into the kitchen and cooking yourself—with more control and flexibility.
    The Shell is your guide, helping you with the recipes (commands).

Using CLI in Linux is powerful, efficient, and gives you full control—just like you see in hacking scenes in movies. In this guide, you'll learn how to use the Linux shell and even write your own shell scripts!

Q&A

Who is the facilitator between the user and the OS?

Answer: Shell

How To Interact With a Shell?

Most Linux distributions use Bash (Bourne Again Shell) as the default shell. When you open the terminal, the shell you see depends on your Linux distro.

By default, the terminal starts in your home directory. To check your current location in the system.

pwd → which stands for Print Working Directory.

cd →short for Change Directory.

ls→shows list of Directory.

cat→ to display file content.

grep→to search any line in a big file.Example: grep (keyword) filename.txt

Q&A

What is the default shell in most Linux distributions?

Answer: Bash

Which command utility is used to list down the contents of a directory?

Answer: ls

Which command utility can help you search for anything in a file?

Answer:grep

Types of Linux Shells

Linux has different types of shells available, each with its own features and characteristics.

echo $SHELL→to see which shell you are using.

cat /etc/shells→to list down the available shells in your Linux OS.

chsh -s→ uses to select any shell use in default.example:chsh -s /usr/bin/zsh

Bash (Bourne Again Shell) is the default shell in most Linux systems. It replaced older shells like sh, ksh, and csh, combining their features and adding new ones.

Features of Bash:

  • Powerful for both running commands and scripting.

  • Supports tab completion to auto-complete commands.

  • Keeps a history of commands—use arrow keys to scroll or type history to view them.

Fish (Friendly Interactive Shell) is a user-friendly shell designed to be simple and helpful—especially for beginners.

Key features of Fish:

  • Simple syntax that’s easy to learn.

  • Auto spell correction for commands.

  • Customizable prompt with cool themes.

  • Syntax highlighting to make commands easier to read and spot errors.

  • Also supports scripting, tab completion, and command history.

Z Shell (Zsh) is a powerful, modern shell that combines features from older shells with some unique enhancements.

Features of Zsh:

  • Advanced tab completion and scripting support.

  • Auto spell correction for commands.

  • Highly customizable, though it may run slightly slower due to this.

  • Includes command history and other productivity features.

To use those shells just by command by their name.Example: bash,zsh,fish

Q&A

Which shell comes with syntax highlighting as an out-of-the-box feature?

Answer:fish

Which shell does not have auto spell correction?

Answer:bash

Shell Scripting and Components

A shell script is simply a file containing a series of commands. Instead of typing commands one by one for repetitive tasks, you can write them once in a script and run the script to execute all commands at once. This saves time and automates tasks. All major Linux shells like Bash, Zsh, and Fish support scripting, but scripting isn't limited to just shells—other programming tools can be used too.

nano→to create a file using any text editor for the script.

to save(ctrl+O) then Enter and Exit (ctrl+X).

Scripting

Every shell script should start with a shebang (#!) to specify which interpreter to use. Since we're using Bash, we begin the script with:

#!/bin/bash

Now let’s build a simple script using basic components:

Variables :help store values (like file paths or names) for reuse.

  • read is used to take user input.

  • echo displays text to the screen.

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.

chmod +x→execution permission to script.

Now that the script has execution permissions use ./ before the script name to execute it.

What is a Loop in Shell Scripting?

A loop is used to repeat a set of commands. Imagine you want to send the same message to many friends. Instead of doing it one by one, you can use a loop that repeats the task for each friend in the list.

Here's a basic example using a for loop:

In this loop:

  • i is the variable that takes values from 1 to 10, one at a time.

  • do starts the block of code to repeat.

  • done ends the loop block.

  • echo $i prints the current value of i in each iteration.

This script will display the numbers 1 to 10, one per line.Bellow:

Conditional Statements

The user's name is entered into a variable (examined in the Variables section) by the script above. Starting with if, the conditional statement compares the value of that variable with the string (name); if they match, the user will be shown the secret; if not, they won't. The condition is terminated by the fi.if, then, else, and fi are used to control the logic.

Save the script by pressing CTRL+X. Confirm by pressing Y and Enter.

#→you can use it to command.

Q&A

What is the shebang used in a Bash script?

Answer:#!/bin/bash

Which command gives executable permissions to a script?

Answer: chmod +x

Which scripting functionality helps us configure iterative tasks?

Answer: Loops

The Locker Script

In a bank, a user has a locker. We must have a script in place that authenticates the user before opening the locker in order to safeguard it. The script should request the user's name, company name, and PIN when it runs. The user should either be granted access or deny it based on the information they enter.

Three things are requested by this straightforward script:
1.Your username

2.The name of your business

3.A PIN

It verifies that the data meets certain details once you enter all three:

1.Mash must be the username.

2.Codex must be the name of the business.

3.7385 must be the PIN.

A notification stating that you have been authenticated and are now able to access your locker appears if all the inputs are accurate.
Access is denied if any of the information is incorrect.

Conclusion

we studied the importance of shells and flew to the world of Linux shells, exploring their major types. Shell scripting and its function in task automation became clear to us. Ultimately, we solved a practical lab related to a script that extracts enigmatic objects from various directories and wrote some fascinating shell scripts.

0
Subscribe to my newsletter

Read articles from MD MUNIF MUBTASHIM directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

MD MUNIF MUBTASHIM
MD MUNIF MUBTASHIM

CSE student | Cybersecurity Learner | Red Team Path | Blogging my CTF & hacking journey .