The course is part of this learning path
As the title suggests, this course looks at intermediate-level skills for those who already know a bit about Linux but want to enhance that knowledge. In this course, introduce new concepts such as job control, switching users, and installing software.
- Implement processes and job control, and switch between users
- Install software using RPM- and Debian-based systems
- Anyone with basic knowledge of Linux who wants to learn more
- Professionals who want to learn more about Linux to enhance their career prospects
This is an intermediate-level course so some knowledge of Linux is expected. If you're just starting out, then try our Linux Fundamentals course first.
In this lesson we will be covering the Shell History, how to repeat commands or portions of commands with the exclamation mark syntax, and how to use autocompletion. Each command you Enter into the shell is recorded in your shell history. Having access to your shell history is extremely useful. You can search through your shell history, repeat commands that you've previously entered, and even recall previous commands and change them slightly before executing them again. Not only can this save you time and keystrokes, but it can prevent you from making mistakes by running a previously known good command.
Some shells like bash, keep their history and memory, and only write the history to a file on exit. Common history files include .bash_history, .history, and .histfile. These history files are stored in your home directory. The history command displays the commands in your shell history, it precedes each one of the commands with a number that can be used to reference that command at a later time.
By default, bash retains 500 commands in your shell history. This is controlled by the HISTSIZE Environment Variable. If you want to increase this number, add export HISTSIZE equals sum number, and place that in your personal initialization files. You can use the Exclamation mark history Expansion Syntax to rerun a command by number. Run the history command to get a list of commands that are proceeded by a number.
If you want to rerun command number three in your history, you would type exclamation mark three Enter. Sometimes the exclamation mark is abbreviated or called bang, so if I were to say bang three know that that means the exact same thing as exclamation mark three. If you want to repeat the previous command, you can use bang bang Enter.
If you wanna repeat a command that starts with a certain letter or string, you can Enter bang followed by that string. For example, let's say you want to repeat the cat command you executed a few moments earlier. You can simply Enter bang C Enter. In addition to executing entire commands in your history, you can pull out parts of a command line. The syntax is bang colon number. The bang represents an event, you use the bang syntax we just talked about, bang number, or bang string, or bang bang.
When pulling out words from a previous command you can abbreviate bang bang to just bang. The number in this syntax represents a word on the command line. Zero represents the first word on the command line which is always going to be a command. One represents the second word on the command line. An easy way to think about this is that it represents the first argument to the command. Let's say you execute this command, head files.txt sorted_files.txt notes.txt. To repeat that entire command type bang bang Enter. If you want to pull out the second argument to the previous command, use bang colon two. Vi bang colon two is equivalent to vi sorted_files.text.
In this example, bang colon Zero, is equivalent to head bang colon one is files.text, bang colon two is sorted files.text, and bang colon three is notes.text. Here are two more exclamation marks syntax shortcuts. The first is bang caret, bang caret represents the first argument to a command, it's the same as bang colon one. The next shortcut is bang dollar which represents the last word on the command line.
In this example command line bang caret equals files.txt and bang dollar represents notes.txt. If you wanted to edit notes.txt you could count the number of words on the command line and use vi bang colon three, or save the counting and a character and simply type vi bang dollar. You can search for commands in your history, to start or reverse shell history search type Control-r. Your prompt changes to reflect that you are performing a search, start typing in characters from the previous command that you are searching for.
If the first result returned is not what you're looking for, type Control-r again to get the next previous match. To execute the command as it is simply press the Enter key. If you want to modify the command before executing it, use the left or right arrow keys to start navigating the command line, make your changes and type Enter to execute the command. To cancel your search and return to the prompt type Control-g.
Another way to increase your efficiency at the command line is by using Tab completion. After you start typing a command you can hit the Tab key to invoke Tab completion. Tab attempts to automatically complete partially typed commands. If there are multiple commands that begin with the same string that proceeds Tab, those commands will be displayed. You may need to hit the Tab key twice in order to reveal the possible completion options. You can continue to type and press Tab again, when there is only one possibility remaining pressing the Tab key will complete the command.
Tab completion not only works on commands, but it also works on files and directories. You can even use Tab completion for environment variables or usernames when using the Tilde expansion syntax. Once you spend some time experimenting with Tab completion it will become second nature.
The history command displays a list of commands in your shell history proceeded by a number. To run commands with a number you can type bang and the number, so bang one will run the first command in our history, which is history.
Let's run another command here, HISTSIZE. To rerun the previous command we can use the bang bang syntax, and that displays the command that's going to be executed, then it executes that command and then it returns the output from that command.
Let's repeat the history command, this time let's use the bang and a string syntax so we can use bang H for the most previous command that started with H. If we want to run the echo command, we could use bang two since it's number two in our history list. And then again, it displays what it's going to run, runs it, and then provides the output.
Let's look at this LS command. Again, to run the previous command over again, bang bang. If you want to pull out portions of that command we can use the bang colon and a number syntax. So let's type echo bang colon two to grab the second argument of this command. So LS is zero, files is one, sorted files is two, and notes is three. So echo bang colon two, should return echo sorted_files.txt. And then it prompts the command that it's going to run and then the output of that command.
So if again, if we want to run a command that starts with a particular letter, we could do that again with bang L so let's pull that up, so we've run our LS command again. So if we want to just pull out the last thing on the previous command line, we can use bang dollar. So echo notes.txt, let's pull up our LS command again. If we want to run the first argument to the previous command, we can use echo bang caret, and that pulls out files.txt since it was the first argument on this command line.
Let's run some more commands here echo alpha bravo charlie. This will grab out the first argument to the previous command, which will be alpha. And then we can use the bang syntax to specify an event let's specify the command that began with L so this should be our LS line, and let's pull out two sorted files from it, and then let's just put in some more text here, delta. And then we'll pull out the LS command again, and then the zero, word, or the command itself from that event.
So you can see we have echo, alpha, sorted_files.txt, delta LS. If we want to perform a reverse search we can type Control-r and then you see that the prompt changes to say that you're performing a reverse search. And this is going to search for echo, so I'll start typing an E and C, so it pulls up the previous command, the previous echo command. If that's not the one we're looking for, we can type Control-r again and we can get the next previous command, and then Control-r again. And then if we find the command we want, we can just hit Enter to execute that command.
Let's do the same thing again, take Control-r to go to a different command, and if we wanna modify this command before we execute it, let's just press an arrow key and then edit the command line. So let's take off these items and then press Enter. If you're doing a reverse search and you decide that you really don't want to do that, you can cancel it with Control-g, and that brings you back to your prompt and nothing has been executed.
Let's look at how Tab completion works, let's type the word who, and then hit Tab Tab. So there are two possible expansions here, if I type the letter A and hit Tab, there's only one option and it completes that command, and hit enter and it runs that command. Let's do the same thing with file, so let's type LS file, hit Tab a couple of times and it shows our choices here. So let's do files.txt. Let's show how this can be pretty quick, so if I type LS sorted_files.txt notes.txt and hit Enter, that's fairly fast typing, but if you use Tab completion it could be a lot quicker. So I just typed LS space SO Tab, and then n Tab, and it did the file completion for us. You can also use Tab completion for environment variables.
So we know the hist environment variable, no the HISTSIZE environment variable, controls how many commands are stored in our history. So let's just look at this H-I-S-T and Tab, and then you can see that those are our few options that are in our environment. So I'll just hit S and then press Tab, and it completes it. You also can do the Tab completion with usernames, with the Tilde expansion syntax. So Tilde represents your home directory if you don't give it an argument, so that's home Jason.
If we type in Tilde ADM and hit Tab a couple of times, we can see that there are two users that begin with ADM on our system. So let's just type in I, because we wanna see the admin users home directory. At Tab it autocompletes and we'll press Enter, and then that displays slash home admin user. In this lesson we talked about how shell history is stored and how the number of commands it saves can be manipulated by the HISTSIZE environment variable.
The exclamation marks syntax, though a little complicated is very powerful, you can use it to repeat entire commands from your shell history or extract portions of previous commands. Using the Tab key to perform auto-completion can increase your efficiency, speed up your workflow, and prevent typing mistakes.
Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.