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.
Following are the different examples:
It prints the current date and time.
The user can specify that the standard output be redirected to a file,
$date > file
The user can specify that standard input can be redirected, as in
$sort <file1> file2
Which invokes the sort program with input taken from file1 and output sent to file2
The pipe helps connect a particular program’s output as input to other programs.
$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.
Keywords are words called reserved words. Following is the list of keywords available in the Bourne shell.
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,
$a = 20
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.
$a = 20
$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.
echo "Hello World!"
- Pwd command
- Ls command
- Mkdir command
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
#Script : emp1.sh - Interactive version
#The pattern and filename to be supplied by the user
echo "\nEnter the pattern to be searched : \c"
echo "\nEnter the file to be used :\c"
echo "\nSearching for $pname from the $flname\n"
grep "$pname" $flname
echo "\nSelected records shown above"
Run it, and specify the input accordingly
Enter the pattern to be searched: director
Enter the file to be used: emp2.lst
Searching for director from file emp2.lst
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.
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.
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–