1

I am incredibly sorry if this is a duplicate. This is something that I have had very little success with determining while searching the googles:

https://www.google.com/search?q=difference+between+%24_+and+!%24

The best that I can figure is that this !$ is preferred for making your work more evident in command history and in shell output.

Example:

02:05 Mac Shell: ~/>$ cat $_
this is a file called something
02:06 Mac Shell: ~/>$ cat !$
cat $_
this is a file called something
02:06 Mac Shell: ~/>$ cat something
this is a file called something
02:06 Mac Shell: ~/>$ cat !$
cat something
this is a file called something
02:06 Mac Shell: ~/>$ echo "This is a string"
This is a string
02:08 Mac Shell: ~/>$ echo !$
echo "This is a string"
This is a string
02:08 Mac Shell: ~/>$ echo "This is a string"
This is a string
02:08 Mac Shell: ~/>$ echo $_
This is a string
02:08 Mac Shell: ~/>$ history | tail
  857  30/12/16 02:05:55 vim something
  858  30/12/16 02:06:07 cat $_
  859  30/12/16 02:06:12 cat $_
  860  30/12/16 02:06:36 cat something
  861  30/12/16 02:06:40 cat something
  862  30/12/16 02:08:18 echo "This is a string"
  863  30/12/16 02:08:26 echo "This is a string"
  864  30/12/16 02:08:32 echo "This is a string"
  865  30/12/16 02:08:37 echo $_
  866  30/12/16 02:09:14 history | tail
02:09 Mac Shell: ~/>$

Question:

Are there any other differences that I should know about?

Edit:

antak brought it to my attention that $_ is great for one liners whereas !$ is not:

02:35 Mac Shell: ~/>$ echo bar && echo foo$_
bar
foobar
02:35 Mac Shell: ~/>$ echo bar && echo foo!$
echo bar && echo foofoo$_
bar
foofoobar
02:35 Mac Shell: ~/>$
Robert J
  • 840
  • 10
  • 20
  • 1
    `$_` in `echo bar && echo foo$_` expands to `bar`. It's simply the last-argument-of-the-last-command-run. You can't use `!$` here because it's not in the history yet. `!$` works for interactive usage, but isn't suitable for scripts. – antak Dec 30 '16 at 08:29
  • 1
    You may want to have a look at here https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html#Special-Parameters – Inian Dec 30 '16 at 08:33
  • Thanks for great info editing the question now to include this with an example. – Robert J Dec 30 '16 at 08:34
  • @RobertJ: Do you have a question or just another informative? – Inian Dec 30 '16 at 08:37
  • Comparing `echo !$` and `echo $_` respectively after `true || echo abc` is also telling. Since the `echo abc` part isn't run, the two start to differ. – antak Dec 30 '16 at 08:37
  • @Inian I was just hoping to get some information about the differences in these variables *antak* has already shown me one difference that I should be mindful of. – Robert J Dec 30 '16 at 08:39
  • How about you read this? http://stackoverflow.com/questions/41385015/what-is-bang-dollar-in-bash/41385075#41385075 – gniourf_gniourf Dec 30 '16 at 08:43

0 Answers0