1

I wanted to test something in shell and I've typed echo "!!" instead of typical echo "foo".

I didn't get the !! for output. Instead, (and very unexpectedly!) it produced the output of my previous command issued for Bash.

I tried to google the phrases bash !! (and permutations like bash +"!!"), but I couldn't find any reference for this feature.

Can someone tell me something about the exclamation marks' stuff and Bash? At least how to find extra info?

I use Bash 4.2.45 on Linux Mint 16 64 bit.

2
  • This did not just produce the output from the previous command (out of some cache), it actually runs the previous command. – Zelda Feb 9 '14 at 11:06
  • @Zelda No, it doesn't execute the last command, it just inserts it. !! alone on a line will execute the last command, while echo "!!" will just echo the last command. – RalfFriedl Sep 26 '18 at 5:19
4

From man bash, section HISTORY EXPANSION:

Event Designators

An event designator is a reference to a command line entry in the history list. Unless the reference is absolute, events are relative to the current position in the history list.

[...]

!! - Refer to the previous command. This is a synonym for `!-1'

You can turn history expansion off with set +H if you don't like it. I generally prefer to use readline to do history lookups instead.

1

You just need to put the exclamation marks between single quotes.

$ echo '!!'
!!
$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.