Bash Shell

Bash Shell Scripting - The Shell

How does the Shell Work?


The Bash shell is normally invoked by a user via a terminal or console session. However, the most useful aspects of the shell is that it allows you to process files containing commands or instructions. These files are called scripts ("shell scripts"). The basic steps a shell performs are:

Read a command or from a file
Validate the syntax of issued command(s)
Run the command(s)
Output results to screen or other device - location
Repeat Process



Although the above process has been greatly simplified, the shell will often handle far more complex instructions. These can include instructions for programming languages, loops, variables, Logical conditional processing.


What are Commands?


Commands are basically a sequence of characters which are executed when you press the "Enter" or return key. The information entered is processed by the shell. All commands have to follow a strict syntax for the shell to interpret these 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 a line is the command word. What then normally follows are parameters explaining what is required.

Mostly parameters start with a dash "-". These are options that you may require or not. By specifying these, we can select certain functionality from a command or ignore certain functionality. For example the command "ls" can be passed on its own or with a combination of additional parameters:


ls
ls -la
ls -lh
ls -rtl
ls -rtlha


In the above example we used the list command "ls". By itself with no parameters it will list all files in the current directory. However, by passing 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 format, reverse time order, with the human readable option set and would display any hidden files (files that are prefixed with a dot ".").

Parameters that have no leading dash are generally the name of a file.



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 this tutorial.

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. (See our Must Know Commands) for a list of commonly used commands.
Internal or External?

Thankfully there is an easy way to identify what is an internal shell command or an external command. As a rule, 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 built in
john@john-desktop:~$ type cat
cat is /bin/cat
john@john-desktop:~$ type cd
cd is a shell built in
john@john-desktop:~$ type type
type is a shell built in


As you can see from the above example, the "cat" command resides within the directory "/bin". This is an external command. The "cd" command is a "shell" built in command.

To see all the Bash Internal Commands, 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 more information about any of the above listed commands, you can type "help command".


Bash Start Up Files


There are several start-up files that are read each time bash is invoked. These files allow individuals to tailor Bash to their own needs or requirements. Some systems are system wide and some are local (specific to an individual user). For a full overview of these files take a look at our Bash Configuration Files section.