2

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?

Arjan
  • 30,974
  • 14
  • 75
  • 112
bodacydo
  • 390
  • 1
  • 3
  • 14
  • @Hastur no! `$name` doesn't get expanded! – bodacydo Nov 29 '15 at 09:17
  • 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 Nov 29 '15 at 09:25
  • I'm seeing [this](http://i.stack.imgur.com/7KoW8.png) 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 Nov 29 '15 at 09:29
  • 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 Nov 29 '15 at 09:35
  • @bodacydo Please add the version of your shell `bash --version` and the OS. :-) – Hastur Nov 29 '15 at 11:31
  • 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 Nov 29 '15 at 12:16
  • Indeed, posting on multiple sites is not nice, and then not responding for 3 hours is even worse :-( – Arjan Nov 29 '15 at 12:53

1 Answers1

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
  • 13,390
  • 26
  • 45
  • 52
  • I don't see why this would stop the [bash history expansion](https://www.gnu.org/software/bash/manual/bashref.html#History-Interaction). For me, pasting the above gets me [this](http://i.stack.imgur.com/rGy0Y.png) 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 Nov 29 '15 at 10:16
  • 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 Nov 29 '15 at 10:24
  • 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 Nov 29 '15 at 10:29
  • Also, just typing `!"` and hitting Enter will get you that dummy command then again. History expansing. – Arjan Nov 29 '15 at 10:31
  • 1
    I 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 Nov 29 '15 at 10:31
  • 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 Nov 29 '15 at 10:34
  • I will try it immediately. – karel Nov 29 '15 at 10:35
  • 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 Nov 29 '15 at 10:36
  • Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/32264/discussion-between-karel-and-arjan). – karel Nov 29 '15 at 10:36