Introducing the Bash shell
The GNU Bash is primarily aprogram that interprets commands entered by the user at the prompt. As we learned in the previous Command line syntax and structure section, each command entered by the user can have three parts:
- The command
- The options (beginning with
-
or--
) - The arguments
Each word entered in the shell is separated from the others with a space. Commands are the names of various applications installed on our system, where each command has its own options and arguments.
When you want to execute a command entered at the prompt, theEnter key is pressed. After theEnter key is pressed, output from that command is displayed on the shell, which is followed again by the prompt as shown in the following screenshot:
Each command is entered on a single line; however, if you wish you can enter multiple commands on a single line using the semicolon (;
), which acts as a command separator.
The various functions performed by the shell include the following:
- It provides an interface between the user and operating system
- It is a way for the user to execute commands and other programs
- It acts as an command-line interpreter for commands entered at the command prompt
- Shell also enables the automation of tasks by reading commands from a special text file, known as a shell script
- Shell provides an environment for users and programs running on the operating system
There are multiple types of shell installed on each Linux distribution, with slight differences in features among them. The Bourne shell (sh) is the most primitive, and the Bash shell is the most advanced. The differences between these shells are listed in the following table:
Feature
Bourne
Korn
C
Tcsh
Bash
Background processing
Yes
Yes
Yes
Yes
Yes
Command history
No
Yes
Yes
Yes
Yes
I/O redirection
Yes
Yes
Yes
Yes
Yes
Shell scripts
Yes
Yes
Yes
Yes
Yes
Command alias
No
Yes
Yes
Yes
Yes
File name completion
No
Yes
No
Yes
Yes
Command completion
No
No
No
Yes
Yes
Command line editing
No
Yes
No
Yes
Yes
Job control
No
Yes
Yes
Yes
Yes
Command execution
Now, we will learn about the different features of the Bash shell with which you can reduce errors and increase the speed at which you work on the Terminal.
Tab completion
Linux shell syntax is case-sensitive aswellas space-sensitive, so typing errors are the first major hurdle in learning for any beginner. However, if the tab completion feature is adopted by a beginner, then it makes life very easy and smooth by reducing typing errors to a minimum.
Tab completion enables you to complete commandnamesor file names once you have typed enough characters at the prompt to make it unique. If the characters entered at prompt are not unique, pressing theTabkey twice displays all commands that can begin with the character already entered into the command line. An example of command completion using theTabkey is shown in the following screenshot:
The Tab completion feature can be used to complete file names or path names when typing them as argument to commands. Pressing the Tab key once completes the filename or path if it is unique; otherwise, pressing the Tab key a second time lists all the possible combinations of filenames or path names based on the current pattern. Thereafter, you can type additional characters to make the name or path unique, and press the Tab key again for completion of the command line. An example of path and filename completion using the Tab key is shown in the following screenshot:
Command-line editing shortcuts
Bash has a very useful command-lineeditingfeature that can increase your productivity while working on the Terminal. It enables the user to use some shortcut commands to move around or delete characters on the command prompt.
The following table lists themost usefulcommand line shortcuts available in Bash:
Shortcut
Description
To move the cursor
Ctrl+A
Moves the cursor to the beginning of the command line
Ctrl+E
Moves the cursor to the end of the command line
Ctrl+Left arrow
Moves the cursor to the beginning of the previous word on the command line
Ctrl+Right arrow
Moves the cursor to the beginning of the next word on the command line
To delete characters
Ctrl+U
Deletes the characters from the current cursor position to the beginning of the command line
Ctrl+K
Deletes the characters from the current cursor position to the end of the command line
Ctrl+W
Deletes the last word from the current cursor positing on the command line
Ctrl+L
Clears the screen (you can also type the clear
command)
To modify the size of the Terminal window
Ctrl++
Increases the size of the Terminal window
Ctrl– –
Decreases the size of the Terminal window
The history command
The history
command is used to display a list of previously executed commands prefixed with a command number showing the order of their execution, as shown in the following screenshot:
The exclamation point character (!
) is a metacharacter in Bash,used forpreviously executedcommandexpansionfrom history liston prompt.
The following table lists varioushistory
commands that are quite useful for beginners:
Command
Description
!<number>
Expands to the command matching the specified number from history
!<string>
Expands to the most recently used command that begin with the string specified at the prompt
history -d <number>
Used to delete the numbered command from history
history -c
Empties the history list
Ctrl+R
Searches the history list of commands for a pattern, and executes the most recent match when found
The following screenshot displays the usage of the history
command:
Besides the already listed options, we can use the arrow keys for navigation between the previous and next command line in the shell’s history. The Up arrow key brings up the previous command executed from the history list. The Down arrow key brings up the next command from the history list.
Command aliases
The alias
command is used to create an alias name or nickname for frequently used commands. It simplifies the administration process by providing alias names for long commands or even combinations of commands.
Listing current aliases
To list the currently configured aliases for your shell, just type alias
without any argument at the prompt, as shown in the following command line:
$ alias
Setting an alias
The following syntax is used to set an alias x
for the exit
command. Thus, after setting this alias, whenever you want to exit from Terminal, you just have to enter x
at the prompt:
$ alias x="exit"
$ alias c="clear"
Removing an alias
To remove an alias
, the unalias
command is used. For example, to remove the previously setalias
, we use the unalias
command as follows:
$ unalias x
Note:
The alias
command will set its alias for the current session only. If you want to set an alias for any command persistently, you have to make an entry for it in /etc/bashrc
for system-wide changes, and if you want to make user-specific changes, than put its entry in the .bashrc
file stored in the user’s home directory.