We can say, the shell (also termed as the command line interpreter) is as old as the Unix operating system. The first Unix shell, called the Thomson Shell, was developed by Kenneth Thompson in 1971. However, with time and technology, the idea of shell has propagated to the presently available number of shells which we use so commonly and easily on Linux Operating System. This article is dedicated to understanding the current definition of shell and its supported features and functionality.
A shell in present world is a program which parses and runs supported commands along with several other features to assist in general and routine use in Linux. The way these commands are run in most of the cases, is by passing them to the operating operating system to execute the corresponding executable.
Now, one may think, the above explanation boils down to a Linux terminal. Then why are we calling it a shell every time. Well, Linux shell is different from a Linux terminal the same way, a program varies from its GUI interface. Hence, a terminal just gives us a medium to access and use the Linux shell. It could be a KDE terminal (Konsole) or a Gnome terminal (terminal).
Once the Linux Operating system boots successfully, check out for a terminal emulator. It looks like following figure 1.1
Types of Shells available
In Linux itself, there could be different shells depending upon how they are implemented and the commands they support. Here is a broad list of generally available ones:
- Bourne Shell
- Korn Shell
- Bourne Again Shell (Bash)
These Linux shells differ in their own ways in terms of support and features available, as in, Username-lookup, history, aliases. Don’t worry if you don’t understand the jargon. You’ll get familiar as you read, learn and self explore Linux Shell.
The following table taken from this link that summarizes the basic differences between all the available shells. One can refer this table to decide which one suits the best.
sh csh ksh bash tcsh zsh rc es Job control N Y Y Y Y Y N N Aliases N Y Y Y Y Y N N Shell functions Y(1) N Y Y N Y Y Y "Sensible" Input/Output redirection Y N Y Y N Y Y Y Directory stack N Y Y Y Y Y F F Command history N Y Y Y Y Y L L Command line editing N N Y Y Y Y L L Vi Command line editing N N Y Y Y(3) Y L L Emacs Command line editing N N Y Y Y Y L L Rebindable Command line editing N N N Y Y Y L L User name look up N Y Y Y Y Y L L Login/Logout watching N N N N Y Y F F Filename completion N Y(1) Y Y Y Y L L Username completion N Y(2) Y Y Y Y L L Hostname completion N Y(2) Y Y Y Y L L History completion N N N Y Y Y L L Fully programmable Completion N N N N Y Y N N Mh Mailbox completion N N N N(4) N(6) N(6) N N Co Processes N N Y N N Y N N Builtin artithmetic evaluation N Y Y Y Y Y N N Can follow symbolic links invisibly N N Y Y Y Y N N Periodic command execution N N N N Y Y N N Custom Prompt (easily) N N Y Y Y Y Y Y Sun Keyboard Hack N N N N N Y N N Spelling Correction N N N N Y Y N N Process Substitution N N N Y(2) N Y Y Y Underlying Syntax sh csh sh sh csh sh rc rc Freely Available N N N(5) Y Y Y Y Y Checks Mailbox N Y Y Y Y Y F F Tty Sanity Checking N N N N Y Y N N Can cope with large argument lists Y N Y Y Y Y Y Y Has non-interactive startup file N Y Y(7) Y(7) Y Y N N Has non-login startup file N Y Y(7) Y Y Y N N Can avoid user startup files N Y N Y N Y Y Y Can specify startup file N N Y Y N N N N Low level command redefinition N N N N N N N Y Has anonymous functions N N N N N N Y Y List Variables N Y Y N Y Y Y Y Full signal trap handling Y N Y Y N Y Y Y File no clobber ability N Y Y Y Y Y N F Local variables N N Y Y N Y Y Y Lexically scoped variables N N N N N N N Y Exceptions N N N N N N N Y
Key to the table above.
Y Feature can be done using this shell.
N Feature is not present in the shell.
F Feature can only be done by using the shells function mechanism.
L The read line library must be linked into the shell to enable this Feature.
Notes to the table above
1. This feature was not in the original version, but has since become almost standard.
2. This feature is fairly new and so is often not found on many versions of the shell, it is gradually making its way into
3. The Vi emulation of this shell is thought by many to be incomplete.
4. This feature is not standard but unofficial patches exist to perform this.
5. A version called ‘pdksh’ is freely available, but does not have the full functionality of the AT&T version.
6. This can be done via the shells programmable completion mechanism.
7. Only by specifying a file via the ENV environment variable.
Besides, Linux also categorizes its shells into types such as restricted and remote shells. Restricted shells (rssh) are the ones which provide limited privileges to the shell end users by the administrator. Limited privileges maybe limited command set.
Remote shell (rsh) as the name suggests are the shells which are useful for remote access to a Linux Machine. It connects to the host machine and sends them commands.
Getting started with Shell
As you access the shell through terminal, you see a cursor which continuously prompts you to write something. You need not worry, its just doing its job. After all, shell understands only what we write, and what we write are the commands. Lets get started with writing and learning a few commands. We shall be discussing a few standard Linux commands.
Before we begin anything about this command, just try it. Type “ls” on your terminal and press enter. In my system, I see
aprograms Documents Music Public TPWQ_C_Rupali
Yes, it lists all the files and directories on the path you are at. That’s exactly what this command does. There are a few options available with this command like:
This lists the files and directories with all their attributes, as in (output in my system):
total 64 drwxrwxr-x 6 entropic entropic 4096 Oct 8 19:53 aprograms drwxr-xr-x 2 entropic entropic 4096 Sep 14 21:07 Documents drwxr-xr-x 2 entropic entropic 4096 Sep 5 21:19 Music drwxr-xr-x 2 entropic entropic 4096 Sep 5 21:19 Public drwxrwx--- 3 entropic vboxsf 4096 Oct 3 12:51 TPWQ_C_Rupali
This includes the hidden files and directories too, as in (output in my system):
. .fontconfig pk .. .gconf .profile .adobe .gnome2 Public aprograms .pulse .bash_history .bash_logout .bashrc Documents .cache .java TPWQ_C_Rupali .config .local .macromedia .mozilla .vimrc Music
For more options, you can refer to its man page
or its complete usage through
To see, which directory you are in, type
For brevity and convenience of future reference, I am listing most of the commonly used commands in Linux. Note, all the options and usage details should be taken from man page or help (using –help option).
|ls||Lists current directory contents||$ls|
|mkdir||creates a new directory||$mkdir mydir|
|cd||change directory||$cd mydir|
|pwd||present working directory||$pwd|
|cp||copy the source to destination||$cp sourcefile destfile|
|mv||move the source to destination||$mv sourcefile destfile|
|rm||remove the file/directorty||$rm myfile
$rm -r mydir
|cat||displays the contents of the file||$cat myfile|
|man||opens the manual page||$man cat|
|echo||display a line of text||$echo “Linux Shell”|
|export||set a value for environment/local/system variables||$export VAR=1|
|find||find files||$find mydir -name myfile|
|grep||find a text in files||$grep “linux” mydir/|
|ps||list current processes||$ps|
|date||shows today’s date||$date|
|time||shows current time||$time|
|clear||clears the terminal||$clear|
|fg||switches to foreground job||$fg|
|bg||switches to background job||$bg|
|history||history of commands||$history|
|exit||exits the shell and closes the terminal.||$exit|
Worth mentioning, another interesting command to know which shell one is working on is is
$ echo $SHELL
In my case, output is
Check out yours?
Highlights and Conventions of Linux Shell
Every shell provides certain set of features, which I call highlights. They are not standard but depends on the implementations and are there to make our experience really convenient and smooth. Its actually fun to use these, so here they are:
(Note: All these highlights are well tried on bash shell )
Many times we need to provide paths or maybe absolute paths in the commands, however typing the complete or maybe relative paths could be pain. As a relief, Linux bash shell comes with an auto-complete option. As you type each letter and press tab, it searches the relevant options available and tries to complete depending upon the feasible options.
If it has just one feasible option, it auto-complete. In other cases, it just lists the feasible options for us to type the next letter. This also works for the commands. For example, try
Aliases are used to abbreviate our long commands if they are frequently used. For example, if we use clear command a lot, we can alias it to just ‘c’.
Now, whenever you type ‘c’, it clears the terminal screen.
Directory Separator [ / ]
This is conventional to Linux Shell rather than a highlight to use forward slash ‘/’ as a directory separator.
Command Separator [ ; ]
Multiple commands can be run at one go separating each with a semi-colon (;).
$mkdir mlb; cd mlb; ls
Pipes[ | ]
Output of one command can be given as input to another command. This is done through pipe. Can be understood better with an example.
$ls | grep mlb
It lists the files/directories in current directory and searches for text “mlb” in the output.
Executing commands by taking the input parameters from the standard input.
$cat myfile | xargs mkdir
The contents of myfile is “mlb”, so above command create a directory of name “mlb”.
Running executable [ ./ ]
An executable in the current directory is run by
However, there is a way to avoid prefixing ./ while running an executable. If we add the path of our executable to the Linux Environment variable “PATH”, executable can be run from any path just like a command. One can find more details in http://mylinuxbook.com/linux-shell-environment/
Running executable in bg [ & ]
If we want the executable to run in background, we add ‘&’
A foreground process is one, which is interacting with the use as it is running. One cannot run the next process in the terminal unless the running foreground process is complete or killed. However, a background process runs separate of the terminal/console and needs no further terminal related interaction from the user.
cwd [ . ] parent [ .. ]
The current directory is by convention represented by a dot and if we want to go to parent directory, i.e. one directory up, we use two dots i.e. ..
For example, for current directory,
, for parent directory,
Killing running fg job [ctrl + c]
For killing a foreground job which is running on terminal, we use ctrl + c
There are certain special symbols being used, which have special meanings, a few common ones are:
|Symbol||Meaning and example|
|$||The value of. e.g. echo $PATH|
|$$||Gives Process Id (PID*) of running bash shell. e.g echo $$|
|~||Home directory. E.g. cd ~/mlb|
|“”||Used for strings. E.g. STR=”MLB”|
|-||To previously visited directory. E.g. cd -|
*PID: Every process is identified by a unique Id, which is called process Id or PID in short.
One can also customize the shell as per our needs and requirements. For a simple example, how about we always change directory to my project directory. Therefore, whenever a terminal is opened, I do “cd myprojectpath”. There is a way we can direct the Linux shell to do certain customized things, whenever it opens the terminal.
It is through config files like .rc and profile. In case of bash shell, they are .bashrc and .profile files.
As per bash man page, .profile are for login shells whereas .bashrc are for interactive non-login shells. Well, login shells are the ones, where you either login from a remote shell, or a different console, basically you provide login name and password to access the shell. However, when you are directly logged in to a Linux machine, and open its very own terminal without any login information, it is termed as a non login shell.
Whenever a Linux shell is initiated, a set of homework is done related to config, environment which is specified through these .rc and .profile files. One can always customize these files to customize the shell.
For example, Lets customize our shell to change directory whenever the shell is initiated. This example is with respect to bash shell.
As our first task, we’ll create a directory, called WORKSPACE.
Now. open the .bashrc file to edit
Move to the end of the file and add following line
Save and Close the file. Its done! Lets see, how that works :
- Exit the terminal (Hint: command is ‘exit’)
- Open the terminal again.
Note, the ‘pwd’ is our newly created directory.
Instances of shell
One can open many instances of the shell which are independent of each other.
In case of Ubuntu :
- Opening a new terminal, key combination is “CTRL + SHIFT + N”
- Opening a new terminal tab, use “CTRL + SHIFT + T”
As in, each instance maintains its own session and that’s how it maintains its Linux shell environment. For example, a variable exported in one session cannot be visible to another session. This lets multiple users working comfortably and independently on the same machine. However, changes made on the disk are universal, which is obvious.
A virtual console is a command line shell prompt by multiple access to the Linux Shell, but with no graphical user interface. These consoles are accessed from the system itself, and not remotely. However, they are virtual consoles accessing the same Linux Shell, and can be used simultaneously, even with separate key settings like NUMLOCK, Caps Lock, etc. They use the same machine but provides a multi user environment where multiple users can work on their own virtual console not depending upon others to leave.
The vital use of such virtual consoles comes on front when our system hangs, or some problem occurs in our console where we are not able to do anything. However, in such a scenario, changing the virtual console and debugging the problem is a big rescue.
One can experience the various virtual consoles through :
CTRL + ALT + F1 CTRL + ALT + F2 CTRL + ALT + F3 CTRL + ALT + F4 .. and so on.
These days in Linux, each virtual console are dedicated for a specific purpose like for logs and messages, for installations, etc. These assignments could vary with different flavours of Linux. However, in my case Virtual Console 7(CTRL + ALT + F7) is for graphical display. Find out for yours.
With the end to this article, take home is a basic understanding of Linux Shell, know how and usage of general commands along with conventions and interesting highlights. We also know about the virtual consoles to play around with Linux. Isn’t it interesting how a Linux Shell, a single program helps Linux users in myriads of ways? How about creating our own Linux Shell in C? Stay tuned for more!