Bash an alias using! $

I found out today what I can write !$to get the last argument from the last command executed.

Now I'm trying to create an alias using this shortcut, and it doesn't work at all.

These are the ones I'm trying to create.

alias gal='git add !$'
alias gcl='git checkout !$'
alias sl='sublime !$'

And this is the result of the output when called galorgcl

fatal: pathspec '!$' did not match any files

So it seems that it is !$simply not replaced by the last argument from the last command in this context.

Is it possible?

+4
source share
2 answers

Instead of messing around with the Bash story, you might also want to use the Bash $_internal variable : the corresponding part from the manual:

$_: [...] . [...]

:

$ touch one two three
$ echo "$_"
three
$ ls
$ echo "$_"
ls
$ a='hello world'
$ echo $a
hello world
$ echo "$_"
world
$ echo "$a"
hello world
$ echo "$_"
hello world
$ 

:

alias gal='git add "$_"'
alias gcl='git checkout "$_"'
alias sl='sublime "$_"'
+2

bash builtin history fc:

$ alias re_echo='echo $(fc -ln -2 | awk '\''NR==1 {print $NF}'\'')'
$ echo foo
foo
$ re_echo bar
foo bar
$ re_echo baz
bar baz
$ re_echo qux
baz qux
+2

All Articles