The Complete Guide to Bash Shell – 2022 Update

Bash Shell Complete Guide Techhyme

The shell program is the interpreter through which we communicate with the operating system. As we type in a command at the shell prompt, it is interpreted and passed to the Linux kernel. The kernel tells the computer what to do. Shell is the command interpreter. It is the most widely used utility program in Linux.

Also Read: List of Commonly Used Windows Standard Powershell Verbs

Shell offers the following features to the users.

1) Interactive Environment:

The shell allows the user to create a dialogue between the user and the host system. This dialogue lasts until the user ends the session.

2) Shell scripts

Shell script is the user interface to the computer system for the Linux operating system. The shell contains internal commands which can be utilized by the user. Shell scripts are groups of Linux commands strung together and executed as individual files

3) Input/output redirections

Linux commands can be instructed to take their instructions from files, and not from the keyboard. The shell also allows the user to place the output of commands into a file and not on screen of the terminal. Output can also be redirected to other devices, such as to a printer or another terminal on the network.

4) Piping Mechanism

Linux supplies ‘pipe’ facility that allows the output of one command to be used as input in another Linux command. The Linux file system contains many utilities that are useful for such purpose.

5) Metacharacter facility

Apart from the normal characters found on a keyboard, (letters and digits) the shell provides the use of ‘metacharacters’. these allows the user to apply selection criteria when accessing files, for example the user could access all files that begin with the letter ‘m’.

6) Background processing

The true multi-tasking facilities allow the user to run commands in the background. This allows the command to be processed in background while the user can proceed with other task in foreground. When background task is completed, the user is notified.

There are many number of shells available in Linux, Bourne Shell is the oldest UNIX shell. The default shell on Linux distribution is the GNU Bourne again shell (bash). Shells available include the source version of the C Shell (tch), k shell (pdksh).

Bourne again Shell (bash)

Bourne shell is the oldest UNIX shell. It is the most widely distributed UNIX shell. It is probably present on every existing UNIX system. The original UNIX shell was written in the mid-1970s by Stephen R.

Bourne while he was at AT&T Bell Labs in New Jersey. The Bourne shell was the first shell to appear on UNIX systems, thus it is referred to as “the shell.”

The GNU/ Linux version of the Bourne (sh) shell is GNU Bourne again shell (bash). It is written by Brian Fox and Ramey. It is a feature enriched version of many of the feature found in the c shell (sh) and korn shell (ksh).

The Bourne Again shell, bash, was developed as part of the GNU project and has replaced the Bourne shell, sh, for GNU-based systems like Linux. All major Linux distributions, including Red Hat, Slack ware, and Caldera, ship with bash as their sh replacement.

Korn Shell (ksh)

Korn shell (ksh) is unix shell originally created by Dave Korn, is an also available in an open source form. The public domain free version of the shell created by Eric Gisin is called the public domain korn shell (pdksh).

In march 2000, AT & T released a freely downloadable version of the original korn shell updated by Dave Korn in 1993, called ksh93‘.

c shell

Bill Joy developed the C shell while he was at the University of California at Berkeley in the early 1980s. It was designed to make interactive use of the shell easier for users.

Another design goal was to change the syntax of the shell from the Bourne shell’s older ALGOL style to the newer C style. The open source version of the c shell is tcsh.

Changing the running shell:

To check which shell is running , display the shell you are using. This information is found in SHELL environment variable. Type ‘echo $SHELL‘ at the shell prompt and press Enter. If you are using bash shell system will display the following massage:

[techhyme@localhost /techhyme]$ echo $SHELL
/bin/bash

If you are not using bash shell and want to change to bash shell, then type ‘bash’ at the shell prompt and press Enter. The system will display the following:

[techhyme@localhost /techhyme]$

Check again the shell you are using, type ‘echo $SHELL’ and press Enter.

To display the bash help function, type help and press Enter. The help command lists all the commands that are built into the shell.

Shell Prompt

When you log in to your Linux account, you will see the shell prompt. It will look as follows:

[techhyme@localhost/techhyme]$

The first techhyme is the username and it is followed by the hostname of the computer and the final techhyme is the directory name that user techhyme is in presently. You are automatically placed in your own user account when you first log in to the system.

When you are logged in as root, the shell prompt displayed is changes to use a pound sign (#) instead of the dollar sign ($).

[root@localhost/root]#

Changing the shell prompt:

The shell prompt can be changed to display information such as the username for a user, the directory in which the user is working, the current date and time or some special message. To change the shell prompt for your current login session, follow the steps given below:

1. Display the shell variables. At the shell prompt, type ‘printenv‘ and press Enter. If you cannot read the entire screen, send the printenv command to the less pager.

(Type printenv |less)

2. Look for the PS1 variable. It may look as follows:

PS1=[\u@\r\w]\$

This variable tells the system to display the username for whoever is using the host computer and that user‘s current working directory inside brackets followed by the $ prompt ( or # prompt for the root account).

3. Write down the information from the PS1 variable.

4. Decide how you want the prompt to look.

Following table shows the characters to customize the shell prompt.

Character Code Description
\! Display the number used in the history list to access the command that would be executed at the shell prompt.
\$ Display a $ in the prompt for regular user and a # for the super user.
\\ Display the backslash character
\d Display the current date.
\h Display the hostname of the computer at which you are working.
\t Display the current time.
\u Display the username of the user who is logged in to the system.
\w Display the current working directory.
\xxx Display any special comment.

Example: If you want to change the prompt so that it will display the time, the date, the current working directory and the $ prompt, then type PS1= [\t \d \w]\$‘ and press Enter. The shell prompt will change to display the following: [12:18:40 Sat June 13 techhyme]$

Once you log out of your account, the shell prompt will return to the default shell prompt.