History command

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search

Use history command to display or manipulate the history list. Run the history command to list commands that you have previously entered.

How to list the previous commands you entered



The history command entered by itself lists the previous 29 commands entered as follows:

Linux history command in action

The listing first displays the position of the command in the $HOME/.bash_history file followed by the command.

How do I list the previous 10 commands only?

history 10

Sample outputs:

  713  pwd
  714  lsb_release -a
  715  help history 
  716  date
  717  history 5

How to repeat the last command one time

Type !!:


Say you typed reboot:


You will get an error that read as follows:

User root is logged in on sshd. The reboot must be run as root user

Now recall the last reboot command using sudo as follows:

sudo !!

Sample outputs:

sudo reboot
[sudo] password for vivek: 

How to repeat command number <n> in the history buffer

To repeat command number 72, run:


How to repeat command beginning with the string <string>

To repeat command beginning with the string sudo, run:


To repeat command beginning with the string "z", run:


How to clear the history list by deleting all of the entries

Run the following command

history -c

Getting help on history command

Simply type the following [[help command]:

help history

Sample outputs:

history: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]
    Display or manipulate the history list.
    Display the history list with line numbers, prefixing each modified
    entry with a `*'.  An argument of N lists only the last N entries.
      -c	clear the history list by deleting all of the entries
      -d offset	delete the history entry at position OFFSET.
      -a	append history lines from this session to the history file
      -n	read all history lines not already read from the history file
    		and append them to the history list
      -r	read the history file and append the contents to the history
      -w	write the current history to the history file
      -p	perform history expansion on each ARG and display the result
    		without storing it in the history list
      -s	append the ARGs to the history list as a single entry
    If FILENAME is given, it is used as the history file.  Otherwise,
    if HISTFILE has a value, that is used, else ~/.bash_history.
    If the HISTTIMEFORMAT variable is set and not null, its value is used
    as a format string for strftime(3) to print the time stamp associated
    with each displayed history entry.  No time stamps are printed otherwise.
    Exit Status:
    Returns success unless an invalid option is given or an error occurs.

See also