Unix / Linux – What is Shells? – Tutorialspoint

A shell provides you with an interface to the Unix system. It collects information from you and runs programs based on that input. When a program finishes running, it displays the output of that program.

Shell is an environment in which we can execute our commands, programs, and shell scripts. There are different flavors of a shell, just like there are different flavors of operating systems. Each shell flavor has its own set of recognized commands and functions.

Shell

prompt The command prompt,

$, which is called the command prompt, is issued by the shell. While the message is displayed, you can type a command.

The Shell reads the input after you press Enter. Determine the command you want to execute by looking at the first word of its entry. A word is an unbroken set of characters. Spaces and tabs separate words.

The following is a simple example of the date command, which displays the current date and time − $date

Thu Jun 25 08:30:19 MST 2009

You can customize your command prompt using the PS1 environment variable explained in the Environment tutorial

.

Shell

types

On Unix, there are two main types of shells

− Bourne shell − If you

  • are using a Bourne shell, The $ character is the default message.

  • Shell C: If you use a C-type shell, The % character is the default indicator.

The Bourne

Shell has the following subcategories:

Bourne shell (sh) Korn shell (ksh) Bourne Again shell (

  • bash
  • )

  • POSIX
  • shell (sh) The different type C shells follow − C shell (csh) TENEX/TOPS C shell

  • (
  • tcsh)

The original Unix shell was written in the mid-1970s by Stephen R. Bourne while at AT&T Bell Labs in New Jersey.

Bourne shell was the first shell to appear on Unix

systems, so it is known as “the shell”. Bourne shell

is usually installed as /bin/sh on most versions of Unix. For this reason, it is the shell of choice for writing scripts that can be used on different versions of Unix.

In this chapter, we will cover most of the Shell concepts that are based on Borne Shell.

The

basic concept of a shell script is a list of commands, which are listed in the order of execution. A good shell script will have comments, preceded by the # sign, describing the steps.

There are conditional proofs, such as value A is greater than value B, loops that allow us to go through massive amounts of data, files to read and store data, and variables to read and store data, and the script can include functions.

We are going to write a lot of scripts in the following sections. It would be a simple text file in which we would put all our commands and various other required constructs that tell the shell environment what to do and when to do it.

Scripts and shell functions are interpreted. This means that they are not compiled.

Sample

script

Suppose we create a test.sh script. Note that all scripts would have the extension .sh. Before adding anything else to your script, you should alert the system that a shell script is starting. This is done using shebang construction. For example −

#!/bin/sh

This tells the system that the commands that follow must be executed by the Bourne shell. It is called shebang because the symbol # is called hash, and the symbol ! is called bang.

To create a script that contains these commands, first place the shebang line and then add the commands

− #!/bin/bash pwd ls

Shell

comments

You can put your comments in your

script as follows: # #!/bin/bash # Author : Zara Ali # Copyright (c) Tutorialspoint.com # The script is still here: pwd ls

Save the above content and make the script executable: $chmod

+x test.sh

The shell script is now ready to run −

$./test.sh

After execution, you receive the following output

: /home/amrood index.htm unix-basic_utilities.htm unix-directories.htm test.sh unix-communication.htm unix-environment.htm

Note: To run a program available in the current directory, use . /program_name

Extended

shell scripts

Shell scripts have several necessary constructs that tell the shell environment what to do and when to do it. Of course, most scripts are more complex than the previous one.

The shell is, after all, a real programming language, complete with variables, control structures, etc. No matter how complicated a script becomes, it’s still just a list of commands executed sequentially.

The following script uses the read command that takes keyboard input and assigns it as the value of the PERSON variable and finally prints it to STDOUT.

#!/bin/sh # Author : Zara Ali # Copyright (c) Tutorialspoint.com # The script is still here: echo “What’s your name?” read PERSON echo “Hello, $PERSON”

Here is a sample run of the script −

$./test.sh What’s your name? Zara Ali Hello, Zara Ali$