0

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:

Boda-Cydo-Sith-Computer:~$ age=20
Boda-Cydo-Sith-Computer:~$ name='boda'
Boda-Cydo-Sith-Computer:~$ var="hello! my name is $name! bye!"

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

Boda-Cydo-Sith-Computer:~$ var="hello! my name is $name! bye"

If I press enter again

Boda-Cydo-Sith-Computer:~$ var="hello! my name is $name bye"

If i press enter again it disappears nothing gets output

Boda-Cydo-Sith-Computer:~$ 

If I try this:

Boda-Cydo-Sith-Computer:~$ 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:

Boda-Cydo-Sith-Computer:~$ echo 'hello! my name is $name! bye!'

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

I have it working this way:

Boda-Cydo-Sith-Computer:~$ 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
  • 75,521
  • 93
  • 229
  • 319

1 Answers1

2

If history expansion is enabled (which it usually is, unless you specifically disabled it), ! followed by something that is not whitespace will attempt to find the last command in history that began with the string that follows it.

Thus the only problematic occurrence of ! in your code is in bye!. You can either add a trailing whitespace (which certainly feels hacky, to say the least), or you can put it in single quotes like you did, but there's no need to do this with the other ! (because they are followed by a whitespace), so the command is not really that hard to read / edit:

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

That's all you need. Or, of course, if you do not intend to use the shell's history expansion capabilities, you can always disable it with set +o histexpand and then this won't be a problem.

Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70