Shell - Accessing last commands in history

April 2017




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


Published by jak58. Latest update on March 13, 2010 at 06:42 AM by jak58.
This document, titled "Shell - Accessing last commands in history," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).