Recalling command history

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to: navigation, 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

history

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 http://humdi.net/vnstat/vnstat-1.9.tar.gz
   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:

date
!!

To repeat last command started with ...

Recall the most recent command starting with vn

date
vnstat
ls
ifconfig
route -n
!vn

To repeat a command by its number

Recall to command line number 13:

history
!13

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

man bash
help history


← Create the constants variableHomePath name expansion →