Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • 17
    One effect of !$ expansion vs $_ parameter is that echo $_ always shows in history as echo $_, while echo !$ shows the value that it was expanded to. E.g. echo one two then echo !$, then pressing up shows echo two. Commented Oct 9, 2019 at 17:24
  • Not sure I understand what "only when interactive" applies to. I was just able to do something along the lines of mkdir -p foo && for f in ${array_of_filenames[@]}; do mv $f $_; done in a bash script Commented Aug 18, 2020 at 4:57
  • This is really about the documentation overloading the word "argument" with two different meanings. $_ gets the last command line argument that actually went to the executed command though the execve() call. That happens after the command line is fully parsed, after all expansions etc. But !$ looks at words at the command line really early, before much parsing has happened. It does some things like detecting quotes there, but not much else. Commented May 27, 2022 at 13:13
  • 1
    You can see a difference too with e.g. echo $RANDOM, where $_ would contain the actual value passed, and hence echo $_ would print the same random number again. But !$ would turn into $RANDOM, which would then be expanded as usual, giving a different number. Or with false && echo bar where $_ would give false (the last argument to the last executed command), but !$ would give bar (the last word on the last command line). Commented May 27, 2022 at 13:14