Expansion
History Expansion
- History expansion is performed immediately after a complete line is read, before the shell breaks it into words, and is performed on each line individually
Designators:
- Event Designators - refer to a particular command in history
- start with
!
- start with
- Word Designators - refer to a particular word of a history entry
- often gets combined with Event Designators, which are demarcated by
:
- ex.
!cp:^
finds the most recentcp
command in history and grabs the 1st argument
- often gets combined with Event Designators, which are demarcated by
- Modifiers - modify result of the Event/Word Designator
Commands:
!ls
- execute most recentls
command- ex. if I ran
git log
, then!git
, bash will say "oh ok, you want the last executed bash command", and will rungit log
- ex. if I ran
!?apache
- execute most recent command that contains the keywordapache
.!%
will refer to the word that was matched by the previous!?<pattern>
search
!-3
- execute 3rd most recent command!!
===!-1
(most recent command)
!$
- reuse last part of the most recent command- ex.
less ~/myfile
thenvim !$
will runvim ~/myfile
- ex.
!*
- reuse all parts of most recent command^ls^cat^
- modify the patternls
withcat
in the previous command!!:s/ls -l/cat/
- replacels -l
of previous command withcat
!!:gs/...
for global substitution
!cp:^
- get 1st arg of the most recentcp
command!cp:$
- get last arg of the most recentcp
command!cp:2
- get 2nd arg of the most recentcp
command!cp:*
- get all args of the most recentcp
command
Tilde Expansion
Tilde prefix
The tilde prefix includes all of the characters before the first slash /
~
$HOME
- in reality, this is shorthand for
~kyletycholiz
(the current user)
~+
$PWD
~-
$OLDPWD
- or, the directory we were in previous to our PWD
Children