-
Overview of BASH history
history
-
Basic History Recall
!!
- Previous command!n
- Nth previous command
-
Parameter Recall
!*
- All parameters from the last command, excluding 0th (the command itself)!:n
- Nth parameter from the previous command (index starting from 1)
-
Advanced Recall
!?<search>
- Previous command with the word<search>
in it, if you want to use a command on such a recall you will need to close<search>
with a?
ie.,!?foo?
!<command>
- Previous<command>
From here on any one of !!
, !n
, !<command>
, or !?<search>?
will be referred to as <history ref>
-
Advanced Parameter Recall
<history ref>:<n/^/$/*>
- Nth, first, last, or all parameters respectively of the<history ref>
-
Basic Replace
^foo^bar
- Replace first occurence offoo
withbar
in previous command
-
Advanced Replace
<history ref>:s/foo/bar
- Replace first occurence offoo
withbar
in<history ref>
- Example:
!395:s/sbiddle/shawn/
- Example:
<history ref>:gs/foo/bar
- Replace all occurences offoo
withbar
in<history ref>
-
Modifiers (These are essentially filters applied to a substitution)
<substitution>:t
- The "tail" of the string, ie., the filename in a path- Example:
!cat:t
- Example:
<substitution>:h
- The "head" of the string, ie., the path minus the filename<substitution>:q
- Surround the substitution with single quotes<substitution>:x
- Split the substitution on spaces/newlines and then surround those with quotes
Example:
> cat /foo/bar /bazz/quz
...
> echo !cat:*:x
echo '/foo/bar' '/bazz/quz'
- History Settings
All of these you can set in your .bashrc with
export <setting>=<value>
- HISTSIZE (number) - How many entries to store (defaults to 500)
- HISTCONTROL (string) - Behaviour of the history
- ignoredups - Don't put the command in your history if it's the sam as the previous command
- ignorespace - Don't put the command in your history if there is a leading whitespace. Useful if you want to run some commands without having them be in the bare history file
- ignoreboth - Both of the previous settings.