The Linux shell is a command-line interpreter or interface that provides a way for users to interact with the Linux operating system. It is a powerful tool for performing various tasks, from simple file operations to complex system administration tasks.
The shell acts as an interface between the user and the Linux kernel, executing commands entered by the user and managing the execution of programs.
In this article we will explore Linux shells and shell scripting so before understanding shell scripting we have to get familiar with the following terminologies:
• Kernel
• Shell
• Terminal
What is Kernel?
The kernel is a computer program that is the core of a computer’s operating system, with complete control over everything in the system. It manages the following resources of the Linux system –
• File management
• Process management
• I/O management
• Memory management
• Device management etc.
What is Shell?
A shell is a special user program that provides an interface for the user to use operating system services. Shell accepts human-readable commands from a user and converts them into something that the kernel can understand. It is a command language interpreter that executes commands read from input devices such as keyboards or from files. The shell gets started when the user logs in or starts the terminal.
Shell is broadly classified into two categories –
• Command Line Shell
• Graphical shell
Command Line Shell
Shell can be accessed by users using a command line interface. A special program called Terminal in Linux/macOS, or Command Prompt in Windows OS is provided to type in human-readable commands such as “cat”, “ls” etc. and then it is executed. The result is then displayed on the terminal to the user. A terminal in the Ubuntu 16.4 system looks like this –
Linux command line
In the above screenshot “ls” command with “-l” option is executed. It will list all the files in the current working directory in a long listing format. These files are usually called batch files in Windows and Shell Scripts in Linux/macOS systems.
There are several shells are available for Linux systems like –
• BASH (Bourne Again SHell) – It is the most widely used shell in Linux systems. It is used as the default login shell in Linux systems and macOS. It can also be installed on Windows OS.
• CSH (C SHell) – The C shell’s syntax and its usage are very similar to the C programming language.
• KSH (Korn SHell) – The Korn Shell was also the base for the POSIX Shell standard specifications etc.
Each shell does the same job but understands different commands and provides different built-in functions.
Graphical Shell
Graphical shells provide means for manipulating programs based on the graphical user interface (GUI), by allowing for operations such as opening, closing, moving, and resizing windows, as well as switching focus between windows. Window OS or Ubuntu OS can be considered as a good example which provides GUI to the user for interacting with the program. Users do not need to type in commands for every action. A typical GUI in the Windows system –
GUI Shell
What is a Terminal?
For those having Linux OS, when we click to open the icon having the name “Terminal”, a window is opened, so in simple terms that rectangular frame or window is called a terminal.
The terminal is a program that provides the user with a simple command-line interface and performs the following 2 tasks:
• Takes input from the user in the form of commands.
• Displays output on the screen.
What is Shell Scripting?
A shell script is a file that contains a sequence of commands for a LINUX-based operating system. That is typed into the keyboard one at a time, into a single script. A shell script is usually created for command sequences that a user needs to use repeatedly to save time.
For writing a script we have to create a file using vim or nano editor where we write the script.
• .sh
is an extension of the script file.
• We can run the script by following commands:
• sh script.sh
As a shell can also take commands as input from file, we can write these commands in a file and can execute them in shell to avoid this repetitive work. These files are called Shell Scripts or Shell Programs. Shell scripts are similar to the batch file in MS-DOS. Each shell script is saved with .sh
file extension e.g., myscript.sh.
A shell script has syntax just like any other programming language. If you have any prior experience with any programming language like Python, C/C++ etc. It would be very easy to get started with it.
A shell script comprises the following elements –
Shell Keywords – if, else, break etc.
Shell commands – cd, ls, echo, pwd, touch etc.
Functions
Control flow – if..then..else, case and shell loops etc.
Why do we need shell scripts?
There are many reasons to write shell scripts:
To avoid repetitive work and automation
System admins use shell scripting for routine backups.
System monitoring
Adding new functionality to the shell etc.
Some Advantages of shell scripts
The command and syntax are exactly the same as those directly entered in the command line, so programmers do not need to switch to entirely different syntax
Writing shell scripts are much quicker
Quick start
Interactive debugging etc.
The following script uses the read command which takes the input from the keyboard assigns it as the value of the variable PERSON and finally prints it
Write an Example of If else in Shell Scripting by comparing 2 numbers.
To get input from the user in a shell script, you can use the read
command. Here's an example of a shell script that takes input from the user to compare two numbers:
The read
command is used to prompt the user for input. It takes the entered value and assigns it to the specified variable (n1
and n2
in this case).
- The script then proceeds with the same comparison logic as in the previous example.
When you run this script, it will prompt you to enter the first and second numbers. After entering the values, it will output the result of the comparison based on the user's input.
Thanks for reading this article.
Happy Learning :)