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

时间:2014-02-06 15:31:49

标签: 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\_转义“_”,但它对我不起作用。

1 个答案:

答案 0 :(得分:7)

相反,请使用:

${site}_log

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

另一个例子

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

$ echo "${myvar}5"
hello5
相关问题