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 user of the computer 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 program which is the common output device. That's why it is known as "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:
These shells specifies the manipulation of programs that are based on the graphical user interface (GUI) by permitting for operations like moving, closing, resizing, and opening windows and switching focus among windows as well. Ubuntu OS or Windows OS could be examined as a good example that offers a graphical user interface to the user to interact with the program. Various users don't need for typing in any command for all the actions.
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 a complicated for many beginners due to it is hard to remember several commands. Command-line shell is very dominant and it permits users for storing commands in a file and run them together. In this way, a repetitive action could be automated 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:
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:
Tcsh is an upgraded C shell. This shell can be used as a shell script command processor and interactive login shell.
Ksh means for Korn shell. It was developed and designed by David G. Korn. Ksh shell is a high-level, powerful, and complete programming language and it is a reciprocal command language as well 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 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.
All these shells do a similar job but take different commands and facilitate distinct built-in functions.
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:
It is known as a command prompt and it is issued via the shell. We can type any command while the prompt is shown.
The below is a common data command example that shows the current time and date:
We can also customize our command prompt with the help of PS1 (environment variable).
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. 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 through the users and run them. Although sometimes we routinely wish to run a set of commands, hence, we have to type within the commands all-time inside the terminal.
A shell script includes syntax similarly to other programming languages. When we have prior experience along with a programming language such as C/C++, Python, etc. It will be very easy to begin with it. The shell script combines the below components:
Shell Scripts Need
There are several causes to write these shell scripts:
Shell Script Advantages
Shell Script Disadvantages
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.
It shows the system that several commands that pursue are to be run by the Bourne shell. It is known as the shebang due to 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:
Comments in shell
We can put our comments in our script below:
Make the script runnable and save the content mention above:
Now, the shell script is ready to be run:
Under execution, we will get the result as follows:
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 to do it 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, and so on. Still, it is only the commands list executed sequentially no matter how complex a script gets.
The following script is using the read command that gets the input through the keyboard and appoints it as the variable PERSON value and prints it over STDOUT finally.