[ -f /root/.bash_aliases ] || echo "$_"
gives me ]
I want /root/.bash_aliases
I also tried
[ -f /root/.bash_aliases ] || echo !:2
but it gives me the following result
[ -f /root/.bash_aliases ] || echo /root/.bash_aliases
/root/.bash_aliases
I only want /root/.bash_aliases
Best Answer
That's the kind of thing functions are made for:
Some shells like
es
orzsh
have anonymous functions:I'd avoid using
$_
. It no longer works if you have a DEBUG trap for instance.Note that
!:n
is not the nth argument of the last command, but the nth lexical token of the previous command line (the last line (possibly even multiline) stored into the history). For instance, inThat
!:1
is not expanded tofoo
(the last argument of the last command), norB
(the last argument of theecho
from theecho
command of the previous command line), but$(echo A B)
. Here you'd need to enter:That is, enter it on separate lines. Here, it happens to work (by accident) even though the history line is not fully complete by the time you enter that second line. That same trick doesn't work in
(t)csh
(where history expansions comes from), norzsh
.