1

This question already has an answer here:

If I type the following command in Bash:

echo "dog!!"

I don't get the string dog!!. Instead I get something else.

I tried to echo the string "dog!!" by echoing "dog\!\!, but it doesn’t work either.

How can I use command arguments when there are two exclamation marks?

I want these exclamation marks to be treated as exclamation marks without any special function.

Two exclamation marks "!!" seem to be the last command.

marked as duplicate by Stephen Kitt, muru, Jeff Schaller, Satō Katsura, GAD3R Apr 4 '17 at 11:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5

You need to either escape the exclamation marks, or use single quotes:

echo dog\!\!
echo 'dog!!'

(This doesn’t explain why your echo "dog\!\!" doesn’t work, but it should work, I don’t know why it wouldn’t. Or at least, it shouldn’t act on the history; in Bash it will output dog\!\!.)

  • Thanks. Single quotes seems to work. Escaping the exclamation marks does not work inside double quotes. – Fibaxi Apr 4 '17 at 7:27
  • 1
    On Bash 4.3.30, echo "\!" prints \!, i.e. the backslash stays, so it doesn't work too well as an escape. – ilkkachu Apr 4 '17 at 8:21
  • @ilkkachu that’s what I’m seeing too. – Stephen Kitt Apr 4 '17 at 8:22
  • @StephenKitt What version of Bash are you using? Please include this in your answer. Thank you. – LinuxSecurityFreak Apr 4 '17 at 9:56
4

In Bash, if you want the exclamation marks to be always treated as regular characters and don't need history expansion, you can disable it for the current shell with

set +o histexpand

Or add the command to the usual configuration files, i.e. most likely .bashrc to disable it for all shells.

  • @StephenKitt OK, I didn't read well obviously. He might have included information about where do I specify this option for it to be set generally for all scripts. Very well, we did a good job on taking this apart :) – LinuxSecurityFreak Apr 4 '17 at 10:19
  • @Vlastimil, I think I missed some comments, but I added a note about the usual configuration files. (Bash's logic of which configuration files to read in which situations is a bit odd in my opinion, but it's not the main point here.) – ilkkachu Apr 4 '17 at 12:28
0

On Linux Mint 18.1 based on Ubuntu 16.04 with:

bash --version | head -1
GNU bash, version 4.3.46(1)-release (x86_64-pc-linux-gnu)

The behavior is as follows:

echo dog\!\!

results in:

dog!!
echo 'dog!!'

results in:

dog!!
echo "dog!!"

prints unexpectedly the dog followed by the last command; e.g. if you did ls before, it prints:

echo "dogls"
dogls

On GNU/Linux Debian 9 Stretch there is a slightly newer version:

bash --version | head -1
GNU bash, version 4.4.11(1)-release (x86_64-pc-linux-gnu)

The behavior is as follows:

echo dog\!\!

results in:

dog!!
echo 'dog!!'

results in:

dog!!
echo "dog!!"

prints unexpectedly the dog followed by the last command; e.g. if you did ls before, it prints:

echo "dogls"
dogls

It seems consistent to me.

It may be possible that on old systems with older version of bash it behaves slightly different. But on new systems, we can expect this behavior.

  • Strings in single quotes aren’t expanded, strings in double quotes are, so it’s normal for the history operators to be processed in double quotes. – Stephen Kitt Apr 4 '17 at 10:49

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