Aja*_*mar 2 variables bash heredoc string-interpolation
在我的 bash 脚本中,我有这个:
myapphome=/home/username/Documents/myapp
cat << 'EOT' > "$myapphome"/some.properties
dir.root="$myapphome"/app_data
EOT
Run Code Online (Sandbox Code Playgroud)
预期在 some.properties 中:
dir.root=/home/username/Documents/myapp/app_data
Run Code Online (Sandbox Code Playgroud)
但实际上是这样的:
dir.root="$myapphome"/app_data
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么?我的意思是我想在我的 some.properties 文件中扩展 $myapphome 。
如果您希望 bash 扩展此处文档中的变量,请不要引用终止符:
cat << EOT > "$myapphome"/some.properties
dir.root=$myapphome/app_data
EOT
Run Code Online (Sandbox Code Playgroud)
另外,从 here 文档中删除双引号,它们不会被扩展删除。
见man bash:
如果
EOT是无引号,在这里,文件的所有行进行参数扩展,命令替换和算术扩展,字符序列\<newline>被忽略,\必须使用来引用字符\,$和`。