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?
@Hastur no!
$name
doesn't get expanded! – bodacydo – 2015-11-29T09:17:15.230I'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 withhello! my name is boda! bye!
. Are you sure you didn't writevar="hello! my name is $name! bye" !!
? – Hastur – 2015-11-29T09:25:22.777I'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.377It 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. Maybeset +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.8801
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.267Indeed, posting on multiple sites is not nice, and then not responding for 3 hours is even worse :-( – Arjan – 2015-11-29T12:53:34.760