If you’re like most computer users, you probably use the Linux or macOS terminal to access your computer’s files and settings. But did you know that you can also use your bash history to speed up your work? In this article, we’ll show you how to use your bash history to get the job done faster. First, let’s take a look at what bash history is and what it can do for you. When you type a command in the terminal, bash stores that command and all of its arguments in a history list. This list is stored in memory and can be accessed by pressing the up arrow key on your keyboard. This means that if you type a command like ls -lh , bash will display the contents of your current directory as well as any files that were added since the last time it was run (in this case, since you last logged out). If you want to run a command but don’t remember its full pathname, simply type its name followed by ! (exclamation point) and then its arguments. For example, if I wanted to print my current directory tree but didn’t remember which commands would print directories and which would print files, I could simply type !ls -lh . Now that we’ve covered what bash history is and how it works, let’s see how we can use it to our advantage. The first thing we’ll do is look at some common tasks that are easier with Bash history enabled. For example, suppose that you want to delete all of the files in a directory but don’t want to have to enter each file’s full pathname every time. You could use the rm -rf command with Bash history enabled (assuming that the directory exists). Simply type rm !* followed by the directory name (without any quotes), and Bash will delete all of the files inside of it without having to enter their full paths every time. Similarly, if you want to copy one ..
The bash shell is the standard terminal environment included with most Linux distributions, included with macOS, and available for installation on Windows 10. It remembers the commands you type and stores them in a history file. You probably know a few basics of the bash history, but it’s a lot more powerful than you might realize.
Keyboard Shortcuts
RELATED: The Best Keyboard Shortcuts for Bash (aka the Linux and macOS Terminal)
To scroll through your bash history, you can use a few of bash’s many useful keyboard shortcuts. Press these shortcuts and commands you’ve previously used will appear at the prompt.
Up Arrow or Ctrl+P: Go to the previous command in your history. Press the key multiple times to walk backwards through the commands you’ve used. Down Arrow or Ctrl+N: Go to the next command in your history. Press the key multiple times to walk forwards through the commands you’ve used. Alt+R: Revert any changes to a command you’ve pulled from your history if you’ve edited it on the current line.
Bash also has a special “recall” mode you can use to search for commands you’ve previously run, rather than scrolling through them one by one.
Ctrl+R: Recall the last command matching the characters you provide. Press this shortcut and start typing to search your bash history for a command. Ctrl+O: Run the command you found with Ctrl+R. Ctrl+G: Leave the history searching mode without running a command.
View Your Bash History
You can print your entire bash history to the screen by running a single command:
You’ll see a list of all the commands in your bash history, along with a number to the left of each. The command with a “1” next to it is the oldest command in your bash history, while the command with the highest number is the most recent.
RELATED: Become a Linux Terminal Power User With These 8 Tricks
You can do anything you like with the output. For example, you could pipe it to the grepcommand to search your command history.
You could also pipe it to the tail command to view only a small number of the recent commands you’ve run. For example, the following command would show the last 5 entries in your history.
Run Commands From Your History
Bash can quickly “expand” previous commands, or expand them and modifying them. This feature is known as “history expansion” and uses an exclamation mark, known as a “bang”. Just type them at the prompt and press Enter to run them like you’d run any other command.
To run a specific command from your history by its number, use the following command:
For example, let’s say you wanted to run the 12th command from your bash history. That’s the command with a “12” to the left of it when you run the history command. You’d type the following command.
To re-run the last command you ran, type the following. This has the same effect as pressing the Up arrow once to view the previous command and then pressing Enter.
You can also refer to a command a certain number of lines back. For example, !-2 would run the second to last command you ran. !! means the same thing as !-1 .
This expansion works anywhere on the line. You can add anything you like before or after !! or any of the other expressions in this section. For example, you can type the following command to rerun the last command you ran through sudo, giving it root privileges. This is particularly useful if you forget to add sudo before running a command.
Or, for example, you could rerun the previous command and pipe its output to grep to search for some text.
To search for a command in your history and run it, type the following. This will run the last command that matches the text you specify:
So, if you recently ran a command that began with ping, you can run the following command to search for it. This will search backwards through your history, locate the most recent command that begins with “pi“, and immediately run it:
You can append a :p to any of the above expansions and bash will print the command to the terminal without running it. This is useful if you want to confirm you’re selecting the correct command before you run it.
Reuse Arguments From Your History
Bash also allows you to run a new command, but use arguments from previous commands in your history. This can help you quickly reuse long or complicated arguments without having to retype them.
For example, let’s say you ran the command touch /home/chris/some_long_file_name_you_dont_want_to_type_again . You now want to run the command nano /home/chris/some_long_file_name_you_dont_want_to_type_again. Rather than typing the whole thing from scratch, you could run:
The !$ would make bash automatically fill in the last argument from your previous command.
This only fills in the last argument. So, if you run ping google.com -c 4 and then run ping !$ , this would just expand to “ping 4“.
To fix this situation, you can use the following trick to expand the first argument on the line, rather than the last:
So, if you ran ping google.com -c 4 and then ran ping !^ , bash would expand this to “ping google.com".
To fill in all the arguments used in the previous command instead of just a single argument, you’d use the following:
So, if you ran ping !* instead, bash would automatically fill in all the arguments you used in the previous command.
You can use the same trick you use to run commands from your history to get arguments from them. Just use the following form.
For example, we ran the command sudo hostname ubuntu earlier. If we run the following command, bash will search backwards through the history to find the last command beginning with the letters we type and fill in the argument we specify. So, if we run echo !su:2 , bash will search back to find the last command beginning with “su” and fill in its second argument, which is “ubuntu“.
Other tricks work as you might expect. For example, replacing the number with an asterisk, known as the wildcard, causes bash to fill in all arguments from the command:
Rerun the Previous Command and Modify It
Bash also allows you to rerun the previous command and specify something that should be changed. This can be useful for correcting a typo in a command. For example, the following command will rerun the previous command, replacing the text “abc” in it with the text “xyz“.
For example, if you accidentally ran ping gogle.com, you could then run ^gog^goog and bash would run the command ping google.com instead.
Where Your History Is Stored, and How to Clear It
The bash shell stores the history of commands you’ve run in your user account’s history file at~/.bash_history by default. For example, if your username is bob, you’ll find this file at /home/bob/.bash_history.
Because your history is stored in a file, it persists between sessions. You can run some commands, sign out, come back the next day, and those commands will still be in your history file ready to view and use. Each user account has its own history file with a separate command history.
To clear your bash history, you can run the following command. This erases the contents of your user account’s .bash_history file:
Bash only remembers a limited number of commands by default, preventing the history file from growing too large. The number of history entries bash remembers is controlled by the HISTSIZEvariable. The default is usually 500 or 1000 entries. You can run the following command to view the size of the bash history on your system.
To set your history to zero, run the following command.
For the current session, bash won’t store any history entries unless your run a command like HISTSIZE=1000 to set it back to a certain number of entries.
How to Ignore Spaces and Duplicates
Bash allows you to ignore history entries that begin with a space if you set the HISTCONTROLvariable to ignorespace.
Type a space before a command before running it in the bash shell and the command will run normally, but won’t appear in your history if you have this variable enabled. This allows you to keep your history a bit cleaner, choosing to run commands without them appearing in your history.
Bash also allows you to ignore duplicate commands that can clutter your history. To do so, set HISTCONTROL to ignoredups.
To use both the ignorespace and ignoredups feature, set the HISTCONTROL variable to ignoreboth.
Note that bash variables you set will only persist for the current session. You’ll need to add these to your user account’s .bashrc file to have these values automatically set in every bash session you start, if you prefer that.
The bash shell is a complex tool with many more options than these. Consult the Bash History Builtins and History Expansion sections in the official bash manual more detailed information and other advanced tricks you can use.