Bash history
The last commands executed on the shell are saved in the Bash history. If you want to execute a command later again, you can use the arrow keys, [Ctrl]-[r] (reverse-i-search) or the "!" sign to access previously executed commands (including all options and parameters).
history command
The history command shows all recorded commands. As an optional parameter, the number of commands displayed can be limited to the last commands called (in this example, the last 15 commands executed):
werner@lmde6:~$ history 15 1791 pwd 1792 cd Downloads/ 1793 ls -lah 1794 free -m 1795 cd 1796 history 1797 vi Downloads/testfile.txt 1798 uptime 1799 history | tail 1800 man history 1801 man 1 history 1802 man bash 1803 history --help 1804 history 10 1805 history 15 werner@lmde6:~$
Executing commands again
If you want to call up a command again, Bash history offers several ways to do this.
Arrow keys
The [↑] and [↓] arrow keys can be used to easily search through the last commands called up and execute them again by pressing the [Enter] key.
[Strg]-[r]
If you want to re-execute a longer command quickly and efficiently, [Ctrl]-[r] (reverse-i-search) is very suitable.
After pressing [Ctrl]-[r], the following input prompt appears on the console:
(reverse-i-search)`':
If you now type individual characters ("pst" in this example), the bash history is automatically searched for commands that contain this character string. If the correct command is found ("pstree -C age -h -t" in this example), the command can be called up again without further input by pressing the [Enter] key:
(reverse-i-search)`pst': pstree -C age -h -t
!
If you have previously displayed the list of executed commands using the history command and you want to call up a specific command again (the 1793rd command in this example), you can do this simply by entering "!" followed by the command number:
werner@lmde6:~$ !1793 ls -lah total 240K drwx------ 26 werner werner 4,0K Feb 27 12:58 . drwxr-xr-x 3 root root 4,0K Nov 22 08:21 .. -rw------- 1 werner werner 34K Feb 27 10:57 .bash_history -rw-r--r-- 1 werner werner 220 Nov 22 08:21 .bash_logout -rw-r--r-- 1 werner werner 3,5K Feb 27 11:36 .bashrc drwxr-xr-x 26 werner werner 4,0K Jän 30 12:57 .cache [...]
Change bash history size
The size of the bash history is limited by default:
werner@lmde6:~$ grep HIST .bashrc HISTCONTROL=ignoreboth # for setting history length see HISTSIZE and HISTFILESIZE in bash(1) HISTSIZE=1000 HISTFILESIZE=2000
To remove the limit, set the variables HISTSIZE and HISTFILESIZE to "-1" in ~/.bashrc using an editor such as nano or vim:
werner@lmde6:~$ vi .bashrc werner@lmde6:~$ grep HIST .bashrc HISTCONTROL=ignoreboth # for setting history length see HISTSIZE and HISTFILESIZE in bash(1) HISTSIZE=-1 HISTFILESIZE=-1
By executing the bashrc script, the new settings will take effect in the current shell window:
source ~/.bashrc
Author: Werner Fischer Werner Fischer, working in the Knowledge Transfer team at Thomas-Krenn, completed his studies of Computer and Media Security at FH Hagenberg in Austria. He is a regular speaker at many conferences like LinuxTag, OSMC, OSDC, LinuxCon, and author for various IT magazines. In his spare time he enjoys playing the piano and training for a good result at the annual Linz marathon relay.
|