With the command reuse feature of the bash shell, it is easy to use a previous command again or to see what commands we have recently used.

img

Check history's definition and usage

History File Location

1
echo $HISTFILE
img

the file that records the used commands

History Size

1
2
echo $HISTSIZE
echo $HISTFILESIZE
img

these two commands show the amount of the lines of commands they can keep

History List

1
history
img

shows all recently used commands

Run Previous Command

1
!!    # Ctrl+P can achieve similar results
img

!! reexecutes the ls command

Run Recorded Command

1
2
3
!1    # runs the first recorded command
!-1    # runs the last recorded command
!N    # runs the command on N line

Delete Executed Command

1
2
3
pwd    # this will be the previous command
ls; history -d $(history 1)    # $(history 1) retrieves the last command's line number
!!    # runs the previous command
img

the previous command is pwd not ls

Run Commands Without Saving

1
2
3
echo $HISTFILE
unset HISTFILE
echo $HISTFILE
img

!! reexecutes the ls command

Empty History Records

1
2
history -c    # takes effect after the user logged out
history -w    # saves the changes to the .bash_history file, so the action takes effect immediately

References 9.1 Bash History Facilities, 9.2 Bash History Builtins, 9.3.1 Event Designators

Buy me a coffeeBuy me a coffee