How to write “!” symbol between double quotes in bash?

1

1

I can't figure out how to write ! symbol in bash scripts when putting it in double quotes strings.

For example:

var="hello! my name is $name! bye!"

Something crazy happens if I type the following commands:

$ age=20
$ name='boda'
$ var="hello! my name is $name! bye!"

When I press enter at last command the command repeats itself (types itself) without the last !:

var="hello! my name is $name! bye"

If I press enter again:

$ var="hello! my name is $name bye"

If I press enter again it disappears nothing gets output:

$ 

If I try this:

$ echo "hello\! my name is $name\! bye\!"

Then it outputs: hello\! my name is boda\! bye\!

If I use single quotes then my name doesn't get expanded:

$ echo 'hello! my name is $name! bye!'

Outputs are: hello! my name is $name! bye!

I have it working this way:

$ echo "hello"'!'" my name is $name"'!'" bye"'!'

But it's one big mess with " and ' impossible to understand/edit/maintain/update.

Can anyone help?

bodacydo

Posted 2015-11-29T09:11:40.057

Reputation: 380

Question was closed 2015-11-30T21:01:06.860

@Hastur no! $name doesn't get expanded! – bodacydo – 2015-11-29T09:17:15.230

I'm sorry I cannot reproduce your problem. Now I'm on a computer and after the first three steps if I write echo $var it answer me with hello! my name is boda! bye!. Are you sure you didn't write var="hello! my name is $name! bye" !! ? – Hastur – 2015-11-29T09:25:22.777

I'm seeing this in a fresh Terminal window, after running those 3 commands, @Hastur. (And that Google folder is not the current folder I'm in, but I do sync command history between Terminal windows.)

– Arjan – 2015-11-29T09:29:12.377

It seems related with shell expansion... @bodacydo 1. write the 3 lines + echo $var in a little script and execute it. 2. In a new shell write echo hi enter, and after !! enter. Check if it expand it before executing. Maybe set +o histexpand can help. – Hastur – 2015-11-29T09:35:36.460

@bodacydo Please add the version of your shell bash --version and the OS. :-) – Hastur – 2015-11-29T11:31:59.880

1

duplicate: http://unix.stackexchange.com/q/246170/4667 and http://stackoverflow.com/q/33980965/7552 -- don't post the same question to multiple sites

– glenn jackman – 2015-11-29T12:16:11.267

Indeed, posting on multiple sites is not nice, and then not responding for 3 hours is even worse :-( – Arjan – 2015-11-29T12:53:34.760

Answers

0

Your code will be easier to read if you do it like this:

:~$ name='boda'
:~$ var="hello! my name is $name! bye!" && echo $var
hello! my name is boda! bye!
  • && - The 2nd command echo $var is executed only if the 1st command var="hello! my name is $name! bye!" succeeds.

  • :~$ - It is not typed in this example because it denotes the bash prompt.

  • ! - You don't need to escape the ! in the above command because it is inside a pair of double quote characters.

karel

Posted 2015-11-29T09:11:40.057

Reputation: 11 374

I don't see why this would stop the bash history expansion. For me, pasting the above gets me this I share command history between Terminal windows so even a fresh Bash instance will find some history. Only using set +H as suggested in the duplicate stops this.

– Arjan – 2015-11-29T10:16:29.263

His question was tagged with the linux tag, so the example in my answer was run in Linux in Ubuntu 14.04 (gnome-terminal). I saw a MacOS directory in your screenshot, although I don't know for sure if the screenshot was taken from a Mac computer or if this has anything to do with getting two different results from running the same command on two different computers. – karel – 2015-11-29T10:24:56.813

Just type, including the quotes, "/some/dummy/command", press Enter (you'll get "No such file or directory") and then repeat your steps above? – Arjan – 2015-11-29T10:29:23.180

Also, just typing !" and hitting Enter will get you that dummy command then again. History expansing. – Arjan – 2015-11-29T10:31:11.493

1I repeated the above steps that you mentioned and I got the same output as in my answer again. I am using bash in gnome-terminal in Ubuntu 14.04. My gnome-terminal has all the default settings except for changing the settings for the gnome-terminal window size and color scheme. – karel – 2015-11-29T10:31:14.063

Then I guess your setup is different from the OP's. Without history expansion there is no reason why !" would show any special behaviour, I'd say? Do the original 3 commands from the question even give you the problem that the OP sees? – Arjan – 2015-11-29T10:34:41.727

I will try it immediately. – karel – 2015-11-29T10:35:23.967

Do you have history expansion switched off? What if you type pwd to see the current folder, and then type !p and hit Enter to repeat that command? – Arjan – 2015-11-29T10:36:28.717

Let us continue this discussion in chat.

– karel – 2015-11-29T10:36:59.423