2014-02-06 45 views
3

我有一個腳本本節: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\_轉義「_」,但它對我無效。

回答

7

相反,使用:

${site}_log 

對於bash,它是相同的調用與$var${var}的變量。但是如果你想處理這種情況,括號非常方便。

解釋的問題在快速的時間正確的另一個例子

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

$ echo "${myvar}5" 
hello5 
+1

+1 :) – anubhava