简体   繁体   中英

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";

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM