Javatpoint Logo
Javatpoint Logo

Linux Shell

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:

Linux Shell

Graphical Shells

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.

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 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:

Linux Shell

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. 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

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 strong 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.

All 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
  • Man page completions
  • Auto-suggestions
  • Support for term256 terminal automation
  • Completely scripted with clean scripts

Shell Prompt

It is known as a command prompt and it is 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 at the initial word of our input. A word can be defined as the character's unbroken set. Tabs and spaces separate words.

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).

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. 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:

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 to write 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, so prone to very costly errors.
  • Design flaws in the language implementation and syntax.
  • Slower execution speed.
  • Offer minimal data structure dissimilar other scripting languages.
  • Not well suited for complex and large 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.

For example:

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.


Next TopicLinux Meaning




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA