Bash:如何区分符号字符和变量名称?

标签 bash

我在脚本中有这个部分:

cat <<EOF >> /etc/httpd/vhosts/$site$dom.conf
<VirtualHost *:80>
   ServerName  $site$dom
   ServerAlias  www.$site$dom
   DocumentRoot /site/http/travel/itn
   CustomLog logs/access_$site_log combined
   DirectoryIndex default.php index.php index.html index.phtml index.cgi index.htm
   ScriptAlias /awstats/ /usr/local/awstats/wwwroot/cgi-bin/
   <Directory /site/http/travel/itn >
      AllowOverride All
   </Directory>
</VirtualHost>
EOF

行中:CustomLog logs/access_$site_log combined 解释器似乎将 _log 视为“$site”变量的一部分。 $site 变量是一个动态变量。 我该如何解决?我尝试使用 _$site\_ 转义“_”,但它对我不起作用。

最佳答案

相反,使用:

${site}_log

对于 bash,使用 $var${var} 调用变量是一样的。但是如果您想处理这些情况,括号会非常方便。

另一个例子

$ myvar="hello"
$ echo "$myvar"
hello
$ echo "$myvar5"

$ echo "${myvar}5"
hello5

关于Bash:如何区分符号字符和变量名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21607122/

相关文章:

bash 羊群 : Why 200?

linux - 如何从 4400 万行文件中随机选择 1 万行

regex - Bash for 循环打印出意外的输出,这是为什么?

linux - 将文件名作为脚本中的密码值

bash - 实时读取两个文件

bash - 遍历文件中的行并发送到 redis-cli

bash——在运行之间存储变量的更好方法?

bash - 在 shell 脚本中传递 CMAKE_CXX_FLAGS

bash - 如何使存储在变量中的 bash 颜色代码可供外部脚本使用

json - 使用 dockerspect 中的 jq bash 命令获取网络 IP 地址