Recalling command history

From Linux Bash Shell Scripting Tutorial Wiki
Jump to navigation Jump to search

← Customize the bash shell environmentsHomePath name expansion →

  • Bash keeps a command history in buffer or a default file called ~/.bash_history.
  • The history buffer can hold many commands.
  • Use history command to display a list of command you entered at a shell prompt. You can also repeat commands stored in history.
  • The history command displays the history list with line numbers.
  • By default history is enabled but can be disabled using set builtin command.
  • You can recall the basic command with arrow keys.

See list of executed commands

Type the following command


Sample outputs:

    3  tail -f /var/log/maillog
    4  cat /etc/resolv.conf 
    5  vnstat
    6  vnstat -m
    7  rpm -qa | grep vnstat
    8  yum update vnstat
    9  cd /opt/
   10  wget
   11  tar -zxvf vnstat-1.9.tar.gz 
   12  cd vnstat-1.9
   13  ls
   14  vi INSTALL
   15  make
   16  cd examples/
   17  ls
   18  vi vnstat.cgi 
   19  cd ..
   20  ls
   21  cd cfg/
   22  ls
   23  vi vnstat.conf 
   24  cd /t,
   25  cd /tmp/
   26  yumdownloader --source vnstat
   27  rpm -ivh vnstat-1.6-1.el5.src.rpm 
   28  cd -

Recall commands

Simply hit [Up] and [Down] arrow keys.

Interactively search history

Press [CTRL-r] from the shell prompt to search backwords through history buffer or file for a command:

(reverse-i-search)`rpm ': rpm -ql rhn-client-tools-0.4.20-9.el5

To repeat last command

Just type !! at a shell prompt:


To repeat last command started with ...

Recall the most recent command starting with vn

route -n

To repeat a command by its number

Recall to command line number 13:


See history command help page for more detailed information about the events and usage:

man bash
help history

This is what it prints:

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. Negative
    		offsets count back from the end of the history list
      -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.

← Create the constants variableHomePath name expansion →