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.
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.
You can do anything you like with the output. For example, you could pipe it to the
grepcommand to search your command history.
history | grep your_search
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.
history | tail -5
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
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.
!! | grep 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:
!$ 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 “
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 “
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 “
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 “
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
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
To use both the
ignoredups feature, set the
HISTCONTROL variable to
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.