In this section, 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.
Bash shell and command execution
Tab completion
Linux shell syntax is case-sensitive as well as 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 command names or 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 the Tab key twice displays all commands that can begin with the character already entered into the command line. An example of command completion using the Tab key 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-line editing feature 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 the most useful command 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 for previously executed command expansion from history list on prompt.
The following table lists various history 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 set alias, we use the unalias command as follows:
$ unalias x