history command

LPIC1-101LINUX

It executes the command number 9 of the history list of commands:

$ history

$ !9

Executes the last command you used:

$ !!

This represents only the very last word (argument) of the previous command:

$ !$

  • Example:

    $ mkdir /var/www/html/my_project

    $ cd !$ # This expands to: cd /var/www/html/my_project

Some flags explained

Writes the history of your current session:

$ history -w

Clears history:

$ history -c

Delete the offset, being the number you have in your history for a command:

$ history -d offset

Append history lines from this session to .bash_history:

$ history -a

Synchronize the history lines from several virtual terminals:

$ history -n

If you manually edited the .bash_history file:

$ history -r

Display that line of the history (without executing it):

$ history -p \!28

Display a range of lines from the history:

$ history -p \!2{5..8}

Search the most recent command that starts with "ls" and show the first argument:

$ history -p \!ls:1

Write anything in the history file:

$ history -s Hello

Contact

Email

hello@unixtips.eu

© 2025. All rights reserved.