Hello again! It has been a few weeks since we talked about the AWK command and a couple of weeks since the SED command, I am back again with some more BASH goodness. This time I want to give you some tips to make you a BASH savant. What is a savant? A savant is a learned person or a sage; someone wise in the ways of things. In this case, BASH event designators.
Event Designators basics
I want to talk about event designators and the ways in which they can make your life easier. An event designator is used to refer to a command that is contained in the shell command history. Event designators in BASH start with the ‘!’ character. The most common use of this is to repeat a command by its ordinal position in history. So if I had for instance:
history 4 clear 5 uname -a 6 yum
And I wanted to run the
uname -a command, I could simply type:
!5 uname -a Linux DESKTOP-T6CS8GI 4.4.0-17134-...2019 x86_64 GNU/LINUX
And if I wanted to repeat that command I could run:
Or more simply:
Both of these commands refer to the last item in the history list.
A very useful event designator example
The most useful thing that can be done with event designators is to use them to rerun that command that you forgot to sudo:
cat /etc/shadow cat: /etc/shadow: Permission denied
I always forget this and then I have to arrow up and scroll back to the start of the line, or I can do this:
sudo !! sudo cat /etc/shadow [sudo] password for darealmc:...
More useful examples of event designators
Sometimes there is a command that you know that you ran, but you are not able to remember the whole thing, or you want to repeat the command but you are not in a position to
history | grep for it. You can use an event designator to recall the command based on the string that it started with. Using our previous history,
!un uname -a Linux DESKTOP-T6CS8GI 4.4.0-17134-...Tue Mar 05 18:57:00 PST 2019 x86_64 GNU/Linux
!su sudo cat /etc/shadow
This can be a real time saver if you know the last few commands that are in the shell history.
Another thing that is useful for me is to be able to repeat arguments for commands. If I want the first argument from the last command that I ran,
!^ for example:
$mkdir thisisanimpossiblylongdirectorynamethatIwillneverremmeber $ cd !^ cd thisisanimpossiblylongdirectorynamethatIwillneverremmeber ~/thisisanimpossiblylongdirectorynamethatIwillneverremmeber$
And this also works for the last item in the last command using
$ cp somefile somedirectory/ $ cd !$ cd somedirectory/
If we take all of the things that we have learned about event designators and we run the following:
$ touch thisisareallylongfilename $ mkdir thisisareallylongdirectoryname $ cp !-2^ !^ cp thisisareallylongfilename thisisareallylongdirectoryname $ cd !$ cd thisisareallylongdirectoryname $
The only sneaky thing here is the use of
!-2^ which is the first argument from 2 commands ago.
Shell like a savant
That will about do it for this session of BASH basics. Event designators, together with the AWK command and the SED command, are some of the most useful things that I have found in BASH and I hope that you find them as useful as I do. Remember that BASH skills are something that separates the masters from the padawans when it comes to system administration.
If you need more assistance with your journey to be a savant, learn more about conditions in bash scripting (if statements) here or check out this great course on BASH scripting for System Administrators! Thanks for joining me and I will see you in the next one!