The Bash shell provides command-line tools for editing and manipulating a user's command history. This is primarily a convenience, a means of saving keystrokes.
Bash history commands:
history
fc
bash$ history 1 mount /mnt/cdrom 2 cd /mnt/cdrom 3 ls ... |
Internal variables associated with Bash history commands:
$HISTCMD
$HISTCONTROL
$HISTIGNORE
$HISTFILE
$HISTFILESIZE
$HISTSIZE
$HISTTIMEFORMAT (Bash, ver. 3.0 or later)
!!
!$
!#
!N
!-N
!STRING
!?STRING?
^STRING^string^
Unfortunately, the Bash history tools find no use in scripting.
1 #!/bin/bash 2 # history.sh 3 # A (vain) attempt to use the 'history' command in a script. 4 5 history # No output. 6 7 var=$(history); echo "$var" # $var is empty. 8 9 # History commands are, by default, disabled within a script. 10 # However, as dhw points out, 11 #+ set -o history 12 #+ enables the history mechanism. 13 14 set -o history 15 var=$(history); echo "$var" # 1 var=$(history) |
bash$ ./history.sh (no output) |
The Advancing in the Bash Shell site gives a good introduction to the use of history commands in Bash.