在cat命令中逃离Unix中的美元符号

Iro*_*low 16 unix bash shell escaping sh

在Ubuntu上,我想得到一个磁盘文件:

foo $(bar)
Run Code Online (Sandbox Code Playgroud)

我想使用cat命令创建此文件,例如

cat <<EOF > baz.txt
type_magic_incantation_here_that_will_produce_foo_$(bar)
EOF
Run Code Online (Sandbox Code Playgroud)

问题是美元符号.我尝试了反斜杠,单引号和双引号字符的多种组合,但无法使其工作.

tri*_*eee 29

您可以在here文档中使用常规引用运算符:

$ cat <<HERE
> foo \$(bar)
> HERE
foo $(bar)
Run Code Online (Sandbox Code Playgroud)

或者您可以通过引用here-doc分隔符来禁用扩展:

$ cat <<'HERE'  # note single quotes
> foo $(bar)
> HERE
foo $(bar)
Run Code Online (Sandbox Code Playgroud)