Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping dollar sign and curly brace in bash

I'm making a script that needs to write ${somestring} to a file. The way I figured doing this was by escaping the dollar sign, but when it executes, the string ends up empty.

Is there a way to escape this correctly?

My code:

# \$\{somestring\} works, but leaves the \'s which I don't want.
myvar=("Yes I require multiple lines.
    Test \${somestring} test"); 

echo "$myvar" >> "mytest.conf";
like image 633
Paradoxis Avatar asked Sep 22 '14 14:09

Paradoxis


1 Answers

Just use single quotes, so that ${} won't be interpreted:

$ myvar=5
$ echo 'this is ${myvar}'
this is ${myvar}
$ echo "this is ${myvar}"
this is 5

Note, though, that your approach is working (at least to me on Bash):

$ myvar=("Yes I require multiple lines.
  test \${somestring} test");
$ echo "$myvar" > a
$ cat a
Yes I require multiple lines.
  test ${somestring} test
like image 144
fedorqui 'SO stop harming' Avatar answered Oct 07 '22 04:10

fedorqui 'SO stop harming'