How to use Wildcard Expansion & History in Bash Shell Command

by Wire Tech
Bash Shell Command

Welcome to the fourth article in our Linux Administration Series. Still talking about Bash Shell Command …

Again?  A third article about Bash Shell Command?! Don’t you have anything else other than Bash Shell Command to talk about?

Actually, we have many!! And everything is coming on time. But for now, we are in the foundation phase. The Linux Bash Shell Command will be your living room as a Linux system administrator, and where you will spend most of your time.

Won’t your living room deserve some more time to set up, decorate, and furnish?

Bash Shell Command – Pathname (Wildcard) Expansion

The seventh step in the Bash Shell Command shell expansion operation is the Pathname (or Wildcard) Expansion. In this step, the shell scans the typed command line for any occurrences of either asterisk * , question mark ? , or square bracket [ .

If any of these characters is encountered, the word is treated as a pattern. The shell will then try to replace the pattern with all possible directory names and filenames that match the pattern.

If no match is found, the shell prints an error message that the file / directory doesn’t exist or can’t be opened.

As a general rule, except the special characters used as wildcards, any printable character will match itself. The backslash \ escapes the next character, which means that it forces the shell to treat the following character “literally”.

Special CharacterFunction DescriptionExamples
*The asterisk matches any stringTo list all files / directories starting with err :
ls -d err*
To remove all files under the /tmp directory:
 
rm /tmp/*
Warning: Use the asterisk wildcard with the rm command with utmost care!! If possible try to avoid using both together.
?The question mark matches any single characterWhile ls warn* 
will match all files starting with warn 

ls warn?
will only match files starting with warn but followed by one single character:
[…]The square brackets containing two or more enclosed characters will match any one of the enclosed characters.The following command will match all files starting with n or e: 
ls [ne]*
The following command:
ls file_[bxz]
Will match file_b , file_x , and file_z .
[numX-numY]Matches any digit in the specified range.The following command:
ls warn[3-6]
will match files warn3 , warn4 , warn5 , and warn6
[charX-charY]Matches any character in the specified characters’ range.The command
ls file_[d-h]
will match: file_d , file_e , file_f , file_g , and file_h .
[!…]
Or
[^…]
Matches any characters not in the enclosed list or range.The command
ls file_[!d-h]  is the negative (opposite) of the previous case

It can be written also as:
ls file_[^d-h] with the same effect.

Using Bash Shell Command History

As a system administrator, there would be some cases where you need to go back and review the history of commands executed on one of your Linux servers. You may need to remember how you achieved a specific task in the past, and which command(s) you used to do it.

You may also need to review commands’ history as part of troubleshooting a problem in your Linux server. Finally, you may get into situation that you must check carefully the commands’ history (for one or more users) in order to investigate a security incident, for the possibility of having any sign of sabotage or malicious action.

For all these situations, the ‘Bash Shell Command’ history becomes very important.

In UNIX and Linux shells, there is the command history , that displays the Bash Shell Command history list with line numbers:

image 27

This way, you can see the sequence of Bash Shell Command as they were executed.

If you want to get the last 5 commands only from the history, specify 5 as an argument to the history command:

image 28

From the retrieved list of commands, we can re-execute a command in a simple way. Just type an exclamation mark and type the line number of the command next to it. For example, if the following is the commands’ history:

image 29

To execute the command at line 39 ( cd /var/log/cron ), just type !39 and press Enter.

image 30

Note

The command cd is used to change the current directory.

The number of commands that the Linux shell will keep and remember is configurable by setting the environment variable HISTSIZE.

image 31

This means the shell will remember the recent 1000 commands as a history.

Recalling Recent Commands

You can recall the most recent command executed at your shell by pressing the keyboard up arrow key . Pressing the up arrow key again will retrieve the command before the last, and so on. To move again to the most recent commands, do the opposite: press the down arrow key ↓ .

When the command you want is recalled, you can press Enter to execute it again, or edit it before re-execution.

Writing Two or More Commands on the Same Line

So far in this series, we used to enter Linux commands one per line. Actually, you can write two, three, or more commands in one line, and execute them all in sequence. To do, you need only to separate commands by semi-colon ;

For instance, the following line contains the commands date, echo, and hostname:

date ; echo ; hostname

Inside a shell script, it will be exactly equivalent to executing the commands one per line:

date

echo

hostname

Executing Multiple Commands as One Unit

There may be cases when you need to run multiple commands, and take their outputs together and treat as one unit. i.e. as if they were one command.

To achieve this, the commands are grouped inside parentheses ( ) and separated by semi-colon. When Seen by the shell interpreter, the shell opens a sub-shell, executes the commands in it, and return back with the aggregate result. Commands grouped within rounded brackets ( ) are executed first, and got their result expanded (substituted).   

The syntax for grouping three commands will be:

( command1 ; command2 ; command3 )

Commands’ grouping is useful in some cases when we need to redirect the combined outputs to a file, or to use them as input to another command (Piping).

List of Bash Shell Command

Okay, that is it for Linux Shell. In the next article, we will tackle a new topic in Linux Administration. See you there.

You may also like

Unlock the Power of Technology with Tech-Wire: The Ultimate Resource for Computing, Cybersecurity, and Mobile Technology Insights

Copyright @2023 All Right Reserved