History Command in Linux (Bash History)

By 

Updated on

8 min read

Linux History Command

If you spend a lot of time on the command line, viewing the history of the commands you have previously run can make your day-to-day work easier and improve your productivity.

In this article, we will cover the history command, which allows you to view a list of previously executed commands, search through the list, and manipulate the history file.

Using the history Command

history is a shell builtin, and its behavior may slightly differ from shell to shell. We will cover the Bash builtin version of history. If you use Zsh, the concepts are similar but the history file defaults to ~/.zsh_history and some options differ.

In its simplest form, when invoked without any option or argument, the history command displays the whole history list with line numbers.

Terminal
history
output
...
467  git push
468  tail -f var/logs/error
469  nano +22,5 functions.sh
470  source project-env/bin/activate
471  history

Typically, history displays many lines of output that don’t fit on the screen. To view the output one page at a time, pipe it to a pager program like more or less :

Terminal
history | less

To display the last n lines, pass the number as an argument to the command. For example, to view only the last five lines from the history list you would type:

Terminal
history 5

Use the Up and Down arrow keys to navigate through previously executed commands. When the command you are looking for appears, press Enter to execute it.

History Expansions

Bash provides several expansion shortcuts to re-run commands from the history list.

Typing !n executes the nth command from the history list, and !-n the command n lines back. In the following example we are executing the command on line 467:

Terminal
!467

!-1 is the same as !! and executes the last command from the history list, !-2 the second to last, and so on.

Type !! to execute the previous command:

Terminal
!!

This is especially useful when you forget to prepend a command with sudo , and instead of re-typing the command you can type:

Terminal
sudo !!

The !word expansion executes the most recent command starting with word:

Terminal
!git

Argument Expansions

You can also reuse arguments from the previous command:

ExpansionDescription
!$Last argument of the previous command
!^First argument of the previous command
!*All arguments of the previous command

For example, if you just ran ls -la /etc/nginx, you can quickly open the same path with:

Terminal
cd !$

Quick Substitution

^word1^word2^ allows you to re-run the last command replacing “word1” with “word2”. If you accidentally typed sduo command instead of sudo command, you can fix it with:

Terminal
^sduo^sudo^

Searching the History

Use the grep command to filter the output. For example, to view all commands including “nano” you would run:

Terminal
history | grep nano
output
302  sudo nano /etc/resolv.conf
356  nano setup.py
413  sudo nano /etc/hosts
469  nano +22,5 functions.sh

Now, if you want to re-run the nano setup.py command simply type:

Terminal
!356

Another way to search through the command history is by pressing Ctrl+R. The prompt will change to the following, and you can start typing to search for a previously executed command:

sh
(reverse-i-search)`':

The shell will display a matching line. To move to the next match, press Ctrl+R again.

Check the Bash manual for more information about History Expansion , modifiers, and designators.

Editing History with fc

The fc builtin lets you open a previous command in your default text editor ($EDITOR), edit it, and execute the modified version when you save and close the file:

Terminal
fc

This opens the last command. To edit a specific command by number:

Terminal
fc 467

To list recent commands without editing, use fc -l:

Terminal
fc -l -10

Saving the History List

By default, when starting a new session, Bash reads the history list from the ~/.bash_history file. The list of commands executed in the current session is kept in memory and saved to the file when the session is closed.

If you opened several shell sessions, only the history of the session that is closed last is saved.

The -a option allows you to append the current session history list to the ~/.bash_history file:

Terminal
history -a

The -w option writes the complete history list to the history file.

Terminal
history -w

Clearing History

The history command allows you to clear the complete history list or remove certain parts.

To clear the history list, use the -c option:

Terminal
history -c

To delete a specific line or lines between a starting and ending position from the history list, use the -d option.

For example, to remove the lines between 365 and 375 (including those lines), you would type:

Terminal
history -d 365 375

The range form of -d requires Bash 5.0 or later. On older versions, you can only delete one line at a time.

If you provide only one number to the -d option, the command removes the given line.

When a negative integer is used, the lines are counted back from the end of the history list.

The commands above clear the history list, which is kept in memory, but do not remove entries from the ~/.bash_history file on disk. To clear the file, you need to write the history list to the file:

Terminal
history -c
history -w

Modifying History Behavior

The behavior of the Bash history can be defined using several different environment variables . When modifying the history behavior, set the variables in ~/.bashrc or any other configuration file which is loaded when the user logs in.

By default Bash keeps 500 lines in the command history list, though many distributions override this to 1000 or more. The HISTSIZE variable controls how many commands are kept in memory during a session. To set it to 10000 add the following line to your ~/.bashrc file:

sh
HISTSIZE=10000

The HISTFILESIZE variable controls the maximum number of lines stored in the history file on disk. When a session ends, Bash truncates the file to this many lines. To keep the same number as HISTSIZE:

sh
HISTFILESIZE=10000

If HISTFILESIZE is unset, the history file is never truncated.

The HISTFILE variable defines the path to the history file. By default it is set to ~/.bash_history:

sh
HISTFILE=~/.bash_history

The HISTCONTROL variable accepts a colon-separated list of values that define how the commands are saved in the history list:

  • ignorespace - commands that start with space are not saved in the history list.
  • ignoredups - duplicate commands are not saved.
  • ignoreboth - is a shorthand, including both ignorespace and ignoredups.
  • erasedups - removes all previous occurrences of the command from the list before saving it.
sh
HISTCONTROL=ignoreboth

The HISTIGNORE variable is a colon-separated list of patterns used to decide which commands should be excluded from the history. For example, to ignore ls, cd, and history commands:

sh
HISTIGNORE="ls:cd:history"

When the HISTTIMEFORMAT variable is set, Bash prepends a timestamp of execution for the command on each line.

For example, if you set:

sh
HISTTIMEFORMAT="%F %T: "

The history will be displayed in the following format:

output
413  2019-10-27 21:13:07: sudo nano /etc/hosts

Sharing History Across Terminal Sessions

By default, when multiple terminals are open, each session maintains its own history in memory. Only the last session to close writes to the history file, overwriting what other sessions saved.

To share history across all open terminals, enable the histappend shell option and configure your prompt to save and reload history:

sh
shopt -s histappend
PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
  • history -a appends new commands to the history file.
  • history -c clears the in-memory history list.
  • history -r re-reads the history file into memory.

Add these lines to your ~/.bashrc to make them permanent.

Security Considerations

Command history can store sensitive data like passwords, tokens, or private URLs. Avoid pasting secrets into the shell, and consider:

  • Setting HISTCONTROL=ignoreboth to skip duplicates and commands starting with a space.
  • Using HISTIGNORE to exclude common sensitive patterns.
  • Removing sensitive entries immediately with history -d 365 followed by history -w.

Quick Reference

TaskCommand
Show full historyhistory
Show last 10 commandshistory 10
Run command by number!467
Run previous command!!
Run previous command with sudosudo !!
Last argument of previous command!$
All arguments of previous command!*
Search historyhistory | grep keyword
Reverse searchCtrl+R
Edit and re-run last commandfc
Append session to filehistory -a
Clear historyhistory -c
Delete a specific entryhistory -d 365

FAQ

How do I prevent a command from being saved to history?
Start the command with a space (e.g., secret-command). This works when HISTCONTROL includes ignorespace or ignoreboth.

Where is the history file stored?
By default it is stored in ~/.bash_history. You can change this with the HISTFILE environment variable.

What is the difference between HISTSIZE and HISTFILESIZE?
HISTSIZE controls how many commands are kept in memory during a session. HISTFILESIZE controls how many lines are stored in the history file on disk.

How do I share history across multiple terminal sessions?
Enable shopt -s histappend and set PROMPT_COMMAND to save and reload the history file on each prompt. See the “Sharing History Across Terminal Sessions” section above.

How do I remove a specific command from history?
Use history -d 365 to delete a single entry, then history -w to persist the change to disk.

Conclusion

The history command displays a list of previously executed commands and provides shortcuts to re-run them quickly. Combined with environment variables like HISTSIZE, HISTFILESIZE, HISTCONTROL, and HISTIGNORE, you can customize history behavior to fit your workflow.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page