Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: How can I distinguish a sign char from the name of the variable?

Tags:

bash

I have this section in a script:

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

In the line: CustomLog logs/access_$site_log combined It seems like the interpreter considers _log as part of the "$site" variable. The $site variable is a dynamic variable. How can I fix it? I tried escaping the "_" by using _$site\_ but it doesn't work for me.

like image 910
Itai Ganot Avatar asked Dec 06 '25 17:12

Itai Ganot


1 Answers

Instead, use:

${site}_log

For bash, it is the same calling a variable with $var or ${var}. But brackets are very handy if you want to handle these kind of situations.

Another example

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

$ echo "${myvar}5"
hello5
like image 174
fedorqui 'SO stop harming' Avatar answered Dec 09 '25 14:12

fedorqui 'SO stop harming'