Working with the Shell
Welcome to the Shell
If you have ever logged into a Linux system or opened a terminal session, then you have encountered the Linux shell. After logging in, you will be greeted with a prompt. This prompt will be similar to the prompt below. Some prompts will display the hostname of the server you are logged into and also your current location (Normally your home directory). Others simply display your userid and hostname as in my session below:
john@john-desktop:~$ john@john-desktop:~$ echo $SHELL /bin/bash
Most Default shells on Linux use the Bash shell (Bourne Again Shell). A quick way to check if you are using the Bash shell is to simply issue the command echo $SHELL as in the example above. If the response comes back /bin/bash, then you are running the bash shell.
What is a Shell?
Simply a shell is a program that takes your commands from a keyboard and passes them to the operating system to be processed. Basically the shell is an interface to your computer or server. As mentioned earlier, Bash stands for the Bourne Again Shell which is an enhanced version of the Bourne Shell program 'sh' which was created by Stephen Bourne. Over the years many shells have been created, some of the other main shells that are used are the "C Shell", created at the University of California in Berkeley. The name "C" was used as this shell was based around the "C Programming" language. Another popular shell that is widely used on many "Unix" systems today is the "Korn Shell" by David Korn.
The most common shell on Linux is the "bash shell", however, there are many, many other shells that have been created and modified. On the Ubuntu 14.04 LTS system I am using the following shells are supported:
john@john-desktop:~$ cat /etc/shells # /etc/shells: valid login shells /bin/csh /bin/sh /usr/bin/es /usr/bin/ksh /bin/ksh /usr/bin/rc /usr/bin/tcsh /bin/tcsh /usr/bin/esh /bin/dash /bin/bash /bin/rbash
The above output shows what shells are currently available from the system I am using. The number of shells available to your system may vary.
How does the Shell Work?
A shell is normally invoked interactively by a user via a terminal or console session. However, one of the most important aspects of the shell is its ability to process files containing commands or instructions. These files are called scripts ("shell scripts"). The basic steps a shell performs are:
Validate the syntax of command
Run the command
Output results to screen or other device/location
Although the above process has been simplified, the shell often handles far more complex instructions. These may include instructions for programming languages, loops, variables, Logical conditional processing. Throughout this "Linux Fundamental" guide, our shell of choice will be "Bash".
What are Commands?
Commands are basically a sequence of characters which are executed when you press the "Enter" key. The information you enter is processed by the shell. Obviously the commands have to follow a strict syntax for the shell to interpret these instructions correctly. The first task of the shell is to divide the instructions entered into words. These are normally separated by a space. Generally the first word on the line is the command word. What then follows are normally parameters explaining what is required.
Generally parameters start with a dash "-". These are options that you may require or not. By specifying these parameters, we can select certain functionality from a command or ignore certain functionality. For example:
ls ls -l ls -lh ls -rtl ls -rtlh ls -rtlha
In the above example we used the basic list command "ls". By itself with no parameters it will list all files in the current directory. However, if we pass additional parameters to the command, we can change the way the results are reported back to us. As you can see multiple parameters can be added together. So, the last example "ls -rtlha" would list the same files in the current directory, however, they would be displayed in long listing, reverse time order, with the human readable option set and would display any hidden files (files that start with a dot ".").
Parameters with no leading dash are nearly always file names.
Internal and External Shell commands
The two main types of commands used by a shell are "Internal" and "External".
Internal: These are commands built directly into the shell which can be executed very quickly. There are numerous commands built into the shell. Many of these will be covered in later sections of the "Linux Fundamental" course.
External: These are not commands built into the shell. These commands (programs) are normally located within various directories. These can be system commands or programs that you have created yourself. The shell is capable of calling these programs from the command line or from within a script.
Internal or External?
It is quite easy to determine if a command is classed as internal to the shell or an external program. Generally, external programs and commands are found in directories such as "/usr/bin". However, there is a simple command that can be used to identify these file types. The "type" command:
john@john-desktop:~$ type pwd pwd is a shell builtin john@john-desktop:~$ type cat cat is /bin/cat john@john-desktop:~$ type cd cd is a shell builtin john@john-desktop:~$ type type type is a shell builtin
As you can see from the above example, the "cat" command resides within the directory "/bin". This is an external command.
If you would like to see all the Bash Internal Commands, then you can issue the "help | less" command. Below is only the header section taken from the "help" command.
GNU bash, version 4.2.24(1)-release (i686-pc-linux-gnu) These shell commands are defined internally. Type `help' to see this list. Type `help name' to find out more about the function `name'. Use `info bash' to find out more about the shell in general. Use `man -k' or `info' to find out more about commands not in this list. A star (*) next to a name means that the command is disabled.
To find out more information about any of the above commands, you can type "help command".