简体   繁体   中英

Execute a local bash variable inside double quotes

Hypothetically I have four webservers that I need to add a line of HTML to a file. As you can see I need the integer to appear after cluster=

for i in 01 02 03 04; do ssh web.${i}.domain.com 'echo "<img src=beacon.gif?cluster=${i}>" >> /var/www/index.html'; done

How can this be accomplished? Thanks in advance.

Please note the ' before and after ${i} :

for i in 01 02 03 04; do
    ssh web.${i}.domain.com 'echo "<img src=beacon.gif?cluster='${i}'>" >> /var/www/index.html'
done

Edit: There is a huge difference between quoting in shell and string literals in programming languages. In shell, "quoting is used to remove the special meaning of certain characters or words to the shell" (bash manual). The following to lines are identical to bash:

'foo bar'
foo' 'bar

There is no need to quote the alphabetic characters - but it enhances the readability. In your case, only special characters like " and < must be quoted. But the variable $i contains only digits, and this substitution can be safely done outside of quotes.

我认为应该这样做:

"echo \"<img src=beacon.gif?cluster=${i}>\" >> /var/www/index.html"
for i in 01 02 03 04
do
  ssh web.${i}.domain.com "echo \"<img src=beacon.gif?cluster=${i}>\" >> /var/www/index.html
done

Basically, just use double quotes, but you'll have to escape the inner ones.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM