System ProgrammingGetting Familiar with BASH Shell in Red Hat Linux Administration

Getting Familiar with BASH Shell in Red Hat Linux Administration

Red-Hat-Linux-Administration-Hands-On

I told you that almost all your tasks as a Linux admin can be done from the shell. So, most of your time is spent there. For these reasons, you need to be familiar with the shell programming.
In this article, we are going to get hands on with the bash shell. Wish you nice reading.

Auto Completion of Commands and Filenames
The greatest feature offered by the bash shell is the auto-completion of commands and filenames. This is done using the Tab key. When you type a sequence of characters that uniquely identify a specific command and press Tab, the shell completes the command for you. If the characters typed weren’t enough to identify a specific command, pressing Tab twice displays the list of commands that share the typed common prefix. This is for commands.
1

For files and directories that may be used as arguments to commands, the Tab key also completes (or suggests) the possible filenames for you.
2

Displaying the Commands History
The bash shell maintains a history of the commands entered by the administrator.

Using the history Command
As its name implies, the history command displays the history of commands entered by the user, preceded by line numbers.
3

The line numbers are not intended to be just a serial, but it could help if you decide to re-execute a specific command from history. For example, to run the command lscpu that appears as 6th in the list, this could be easily done like this:
4

Using the Up and Down Arrows
Pressing the Up arrow once retrieves the last entered command.
5

Pressing the Up arrow once again retrieves the second last command used.
6

On the other hand, pressing the Down arrow moves in the history one command forward.

Using the Ctrl Key to Edit or Move within Lines
Using Ctrl+a moves the cursor to the beginning of the line:
7

Using Ctrl+e moves the cursor to the end of the line.
8

Using Ctrl + Right moves one word to the right. Conversely, Using Ctrl + Left Arrow moves the cursor one word to the left.

Using Ctrl+u deletes characters to the beginning of the line. One the other hand, to delete characters to the end of the line, use Ctrl+k.

Wildcard Expansion
Wildcards are a group of characters that have special meanings when used in the shell.

The following table lists some of the commonly used wildcards with a description, and an example for each:

Wildcard Description Example
* The asterisk * matches zero or more characters. To remove all files in the current directory:

rm *
? The question mark matches any single character. The command:

ls file?

will list all files like:
file0  file1  file2  file3  file4  file5  file6  file7  file8  file9
but will not list:
file10  file11  file12  file13  file14  file15  file16  file17  file18  file19  file20

[0-9] A Range of numbers between a pair of square brackets matches any single digit in that range. The command:

ls file[3-7]

will match
file3  file4  file5  file6  file7

[characters_list] A list of characters between a pair of square brackets could match any single character from the list. The command:

ls file[4bdh]

will match
file4  fileb  filed  fileh

[^characters_list] The opposite of the above case. This will match any single character that does not belong to the character list.  

Curly Braces {} Expansions
If you need to generate strings for printing or for further processing, the curly braces expansion using the curly { } brackets is the right solution.

Generating List of Strings
To create several directories using one command statement, the curly braces could help:

mkdir {finance,IT,sales,HR}

Generating a Sequence of Numbers
To generate a sequence of numbers between START and END in steps of one:

{START..END}

For example, the following will print numbers between 1 and 100

echo {1..100}

9

To create a set of files with common prefix file, and a variable suffix, which is a number from 00 to 31:

touch file{00..31}

10

To print numbers from 200 to 100 (in descending order):

echo {200..100}

11

Generating a Sequence of Characters
To print characters from h to q, use the command

echo {h..q}

12

To create a list of directories whose names are A to Z:

mkdir {A..Z}

13

Summary
In this article, we have talked about the main features of the bash shell. We have seen how bash could complete commands and filenames using the Tab key. We have also learned how to check the history of commands entered in the shell. Finally, we talked about the wildcards, and the curly braces expansion. I hope you find this useful. See you in the next article.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -