2021-04-04 21:07:13 +01:00
|
|
|
# history expansion
|
|
|
|
|
2024-03-10 06:53:17 +00:00
|
|
|
> Reuse and expand the shell history in `sh`, Bash, Zsh, `rbash` and `ksh`.
|
2021-04-09 17:46:37 +01:00
|
|
|
> More information: <https://www.gnu.org/software/bash/manual/html_node/History-Interaction>.
|
2021-04-04 21:07:13 +01:00
|
|
|
|
2021-07-05 20:35:47 +01:00
|
|
|
- Run the previous command as root (`!!` is replaced by the previous command):
|
2021-04-04 21:07:13 +01:00
|
|
|
|
|
|
|
`sudo !!`
|
|
|
|
|
|
|
|
- Run a command with the last argument of the previous command:
|
|
|
|
|
|
|
|
`{{command}} !$`
|
|
|
|
|
|
|
|
- Run a command with the first argument of the previous command:
|
|
|
|
|
|
|
|
`{{command}} !^`
|
|
|
|
|
2021-07-05 20:35:47 +01:00
|
|
|
- Run the Nth command of the history:
|
|
|
|
|
|
|
|
`!{{n}}`
|
|
|
|
|
2021-04-04 21:07:13 +01:00
|
|
|
- Run the command `n` lines back in the history:
|
|
|
|
|
|
|
|
`!-{{n}}`
|
|
|
|
|
2021-07-05 20:35:47 +01:00
|
|
|
- Run the most recent command containing `string`:
|
2021-04-04 21:07:13 +01:00
|
|
|
|
2021-07-05 20:35:47 +01:00
|
|
|
`!?{{string}}?`
|
2021-04-04 21:07:13 +01:00
|
|
|
|
|
|
|
- Run the previous command, replacing `string1` with `string2`:
|
|
|
|
|
|
|
|
`^{{string1}}^{{string2}}^`
|
|
|
|
|
|
|
|
- Perform a history expansion, but print the command that would be run instead of actually running it:
|
|
|
|
|
|
|
|
`{{!-n}}:p`
|