Updated June 20, 2023
Introduction to Shell in Linux
Linux is a code that transmits the system commands., Compilers, Editors, linkers, and command-line interpreters are essential and valuable but are not part of the operating system. We will look briefly at the LINUX command interpreter, called the SHELL, which, although not part of the operating system, makes heavy use of many operating system features and thus serves as an excellent example of how the system calls can be used. It is also the primary interface between a user sitting at his terminal and the operating system.
Shell has standard input and output as its terminal. Shell is started when a user begins to login. A dollar sign is typed to start a command, indicating the user that the shell is ready to accept the command. For instance, when a user gives the date as input, a child process is created, and a program begins to run as a child process. Only when the child process is terminated after execution the shell returns to the prompt and type again; after that, it reads the following lines as input.
Examples
Following are the different examples:
Example #1
It prints the current date and time.
Command:
$date
Example #2
The user can specify that the standard output be redirected to a file,
Command:
$date > file
Example #3
The user can specify that standard input can be redirected, as in
Command:
$sort <file1> file2
Which invokes the sort program with input taken from file1 and output sent to file2
Example #4
The pipe helps connect a particular program’s output as input to other programs.
Command:
$cat file1 file2 file3 | sort>/dev/lp
This invokes the cat program to concatenate three files and send the output to sort to arrange all the lines alphabetically. The output of sort is redirected to the file /dev/lp, a familiar name for the special character file for the printer.
Types of Shell
SHELL determines the type of shell that a user sees logging in. Today, a host of the shell accompanies any LINUX system, and you can select the one you like the most. Besides the Bourne shell, the most popular in the LINUX world, the C and Korn shells have also carved out a niche for themselves because of certain inherent advantages. The program csh knows the C shell, the Korn shell by ksh, and the bourne shell by sh.
If you wish to use any of the above shell types as the default shell, the variable must be assigned accordingly. However, after reading a field in the file /etc./passwd, the system makes this assignment. This file must be edited if you wish to change the setting permanently. The system administrator usually sets up your login shell while creating a user account, though you can change it whenever you request.
Shell Keywords
Keywords are words called reserved words. Following is the list of keywords available in the Bourne shell.
Echo | if | until | trap |
read | else | case | wait |
set | fi | esac | eval |
unset | while | break | exec |
read-only | do | continue | ulmit |
shift | Done | exit | umask |
export | For | return |
1. Unchanging variables- set keyword
In some applications, a need for variables to have a constant or fixed value may arise. For instants, if we want that a’s variable should always remain at 20 and not change, we can achieve this by saying,
Example #1
$a = 20
$readonly a
The shell will not permit to change a value when they are created as read-only. To create read-only variables, type “read-only” at a command prompt.
When there is a need to clear or erase a particular command from the shell, we will use the “unset” keyword as a command.
Example #2
$a = 20
$echo a
20
$unset a
$echo a // a have no value now so it prints nothing
2. Echo keyword
To print either the value of any variable or words under double quotation.
Example #1
x=20
echo $x
Output:
Example #2
echo "Hello World!"
Output:
- Pwd command
pwd
Output:
- Ls command
mkdir newdir
ls
Output:
- Mkdir command
mkdir imp
ls
Output:
3. Cd command: read keyword
The read statement is the shell’s internal tool for taking input from the standard input. Functionally, it is similar to the INPUT statement of BASIC and the scanf() function in C. But it has one or two interesting features; it can be used with one or more variables to make shell scripts interactive. These variables read the input supplied through the standard input during an interactive session. The script emp1.sh uses the statement to take the search string and the filenames from the terminal.
Command – Shell in Linux
$cat emp1.sh
#Script : emp1.sh - Interactive version
#The pattern and filename to be supplied by the user
echo "\nEnter the pattern to be searched : \c"
read pname
echo "\nEnter the file to be used :\c"
read flname
echo "\nSearching for $pname from the $flname\n"
grep "$pname" $flname
echo "\nSelected records shown above"
$_
Run it, and specify the input accordingly
$emp1.sh
Enter the pattern to be searched: director
Enter the file to be used: emp2.lst
Searching for director from file emp2.lst
Output:
Read can also make the script pause without accepting any input from the user. It can simply wait for the <Enter> key to be pressed for program execution to continue (somewhat like the WAIT command of dBASE). You can also condense the above program by accepting both arguments in one line; a single read can be used with two or more variable names.
Conclusion
In a Linux/Unix operating system, the function of the shell is to interpret the command output. It acts as an interface to run the commands. Linux and Unix shells have more features than a command Interpreter; it acts as programming language with basic structures like conditional loops, functions, variables, etc. Shell is a powerful environment that can run commands and works as a programming language by executing the input commands. The shell’s wide usage stems from its features, with a Linux/Unix shell possessing more advantages than a Windows shell.
Recommended Articles
This is a guide to What is Shell in Linux? Here we discuss the introduction and types of Shell, Commands, and respective examples. You can also go through our other related articles to learn more–