In bash, how to expand the !$ while typing the command line?
-
27-04-2021 - |
Question
Is there a way to expand the !$
in command line while interactively editing the command inside shell?
For example, while I am typing ls !$
, I press some button and then I see what is the value of !$
.
Solution
Do you want this sequence to be treated literally, without any expansion? You may quote it with apostrophes: '!$'
UPDATE If you want to expand it before executing, you may use Ctrl-Alt-E, but beware that it would perform "word expansion" as well, so !$ "single argument"
would be expanded to expanded_string single argument
(no quotes => two arguments).
You may also use Alt-_ (works in both emacs and vi modes), or Alt-. (works only in emacs mode) to just insert the last argument of the previous command directly, without any expansion.
OTHER TIPS
You can simple use:
!$:p
This will print the most recently executed command's last word.
Or type Ctrl-Alt-e to expand !$
Or type ESC. to get !$
You type !$
and after that you press: Alt-^
and the !$
is expanded immediately.
Thanks to @Alexis for his answer on https://stackoverflow.com/q/9360013/813665 that reveals me the power of Alt-^
.