The shell can be defined as a command interpreter within an operating system like Linux/GNU or Unix. It is a program that runs other programs. The shell facilitates every computer user as an interface to the Unix/GNU Linux system. Hence, the user can execute different tools/utilities or commands with a few input data.
The shell sends the result to the user over the screen when it has completed running a standard output device program. That’s why it is known as a “command interpreter.”
The shell is not just a command interpreter. Also, the shell is a programming language with complete constructs of a programming language such as functions, variables, loops, conditional execution, and many others.
For this reason, GNU/Unix Linux Shell is stronger than the Windows shell.
Broadly, the shell is categorized into two main categories, which are explained below:
Graphical Shells
These shells specify the manipulation of programs based on the graphical user interface (GUI) by permitting operations like moving, closing, resizing, and opening windows and switching focus among windows. Ubuntu OS or Windows OS could be an excellent example of a graphical user interface for the user to interact with the program. Various users don’t need to type in any command for all the actions.
Command-line Shell
Various shells could be accessed with the help of a command-line interface by users. A unique program known as Command prompt in Windows or Terminal in macOS/Linux is offered for typing in the human-understandable commands like “ls,” “cat,” etc., and after that, it is being run. The result is further shown to the user on the terminal.
Working on a command-line shell is complicated for many beginners because it is hard to remember several commands. Command-line shell is very dominant, and it permits users to store commands in a file and run them together. In this way, it could automate a repetitive action easily. Usually, these files are known as Shell scripts in macOS/Linux systems and batch files in Windows.
There are various types of shells which are discussed as follows:
Bash Shell
In the bash shell, bash means Bourne Again Shell. It is a default shell over several distributions of Linux today. It is a sh-compatible shell. It could be installed over Windows OS. It facilitates practical improvements on sh for interactive and programming use, which contains:
- Job Control
- Command-line editing
- Shell Aliases and Functions
- Unlimited size command history
- Integer arithmetic in a base from 2-64
Csh/Tcsh Shell
Tcsh is an upgraded C shell. This shell can be used as a shell script command processor and interactive login shell.
Tcsh shell includes the following characteristics:
- C like syntax
- Filename completion and programmable word
- Command-line editor
- Job control
- Spelling correction
Ksh Shell
Ksh means for Korn shell, and it was developed and designed by David G. Korn. Ksh shell is a high-level, powerful, and complete programming language, and it is a standard command language, just like various other GNU/Unix Linux shells. The usage and syntax of the C shell are very same as the C programming language.
Zsh Shell
Zsh shell is developed to be reciprocal, and it combines various aspects of other GNU/Unix Linux shells like ksh, tcsh, and bash. Also, the POSIX shell standard specifications were based on the Korn shell.
Also, it is a robust scripting language like other available shells. Some of its unique features are listed as follows:
- Startup files
- Filename generation
- Login/Logout watching
- Concept index
- Closing comments
- Variable index
- Key index
- Function index and various others that we could find out within the man pages.
These shells do a similar job but take different commands and facilitate distinct built-in functions.
Fish
Fish stands for “friendly interactive shell.” It was produced in 2005. Fish shell was developed to be fully user-friendly and interactive, just like other shells. It contains some good features, which are mentioned below:
- Web-based configuration
- Manpage completions
- Auto-suggestions
- Support for term256 terminal automation
- Entirely scripted with clean scripts
Shell Prompt
It is known as a command prompt issued via the shell. We can type any command while the prompt is shown.
Shell reads our input after we click Enter. It illustrates the command we want to be run by seeing the initial word of our input. A word can be defined as the character’s unbroken set. Tabs and spaces separate words.
Below is a typical data command example that shows the current time and date:
We can also customize our command prompt with the help of PS1 (environment variable).
Shell Scripting
The common concept of the shell script is the command list. A good shell script will contain comments which are preceded via # simbol.
Shell functions and scripts are interpreted, and it means they aren’t compiled.
There are also conditional tests like value Y is greater than value Z, loops permitting us to proceed by massive data amounts, files to store and read data, and variables to store and read data, and these scripts may contain functions.
The shells are usually interactive, which means they receive commands as input from the users and run them. Although sometimes we routinely wish to run a set of orders, we have to type within the commands all the time inside the terminal.
A shell script includes syntax similarly to other programming languages. When we have prior experience with programming languages such as C/C++, Python, etc. It will be straightforward to begin with it. The shell script combines the below components:
Functions
- Control flow: if, else, then, shell loops, case, etc.
- Shell commands: touch, pwd, echo, ls, cd, etc.
- Shell keywords: break, if, else, etc.
Shell Scripts Need
There are several causes for writing these shell scripts:
- For avoiding automation and repetitive work
- Shell scripting is used by system admins for many routine backups
- Including new functionalities to the shell
- System monitoring etc.
Shell Script Advantages
- The syntax and command are exactly similar to those entered directly in a command line. Thus, the programmers don’t have to switch to completely different syntax.
- Interactive debugging
- Quick start
- It is much quicker to write the shell scripts etc.
Shell Script Disadvantages
- A single error can modify the command, which could be harmful and prone to costly mistakes.
- Design flaws in the language implementation and syntax.
- Slower execution speed.
- Offer minimal data structure dissimilar to other scripting languages.
- Not well suited for complex and extensive tasks etc.
Script Example
Assume we make a test.sh script. We have to alert a system that the shell script is started before we include anything else in our script.
Note: Every script will have a .sh extension.
It can be done with the help of the shebang construct.
- #!/bin/sh
It shows the system that several commands that pursue are to be run by the Bourne shell. It is known as the shebang because the symbol # is known as hash and the symbol “!” It is known as the bang.
To make a script including these commands, we put the shebang construct line first, and after that, add any command:
- #!/bin/bash
- pwd
- Is
Comments in shell
We can put our comments in our script below:
- #!/bin/bash
Make the script runnable and save the content mentioned above:
- $chmod +x test.sh
Now, the shell script is ready to be run:
- $./test.sh
Under execution, we will get the result as follows:
- /home/amrood
- index.htm unix-basic_utilities.htm unix-directories.htm
- test.sh unix-communication.htm unix-environment.htm
Note:- For executing a program present in the latest directory, we can use the ./program_name
Extended Shell Scripts
The shell scripts contain several needed constructs that define the shell platform, when, and what to do. Most of the scripts are more complicated than the above scripts.
After all, the shell is an actual programming language that is complete with control structures, variables, etc. Still, only the commands list are executed sequentially, no matter how complex a script gets.
The following script uses the read command that gets the input through the keyboard, appoints it as the variable PERSON value, and prints it over STDOUT finally.