Shell - Accessing last commands in history

December 2016




The shell Linux/Unix provide several features saving time.

Thus, it is possible to go back in history with the recent orders navigation arrows by pressing the up arrow.

Moreover there are ways to access commands quickly in the history! By typing exclamation point (!) followed by the first letters of the command, the shell automatically runs the last command starting with those letters:
  • Either the following history:


system:/home/user$tail /var/log/syslog 
system:/home/user$vi /etc/apache/httpd.conf 
system:/home/user$/etc/init.d/mysql restart 
system:/home/user$apachectl restart 
system:/home/user$tail /var/log/apache/error.log


The following command allows you to access the command n-4 in the history(vi /etc/apache/httpd.conf)
    • !vi
  • Further application :
    • !! //The last commant
    • !23 //The 23th command
    • !-2 // Execute the last command
    • !?profil //The last command containing the word "profil"
    • ^13^14^ //Substitution in the last command
  • The history of command is registered at each end of session under the file:
    • ~/.bash_history
  • To avoid duplication in this file and history, add the line ~/.bash_profile in the command:
    • export HISTCONTROL=ignoredups
  • You can however display the search command function by pressing CTRL+R

Related :

This document entitled « Shell - Accessing last commands in history » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.