Execute a local bash variable inside double quotes

16,994

Solution 1

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.

Solution 2

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.

Solution 3

I think this should do it:

"echo \"<img src=beacon.gif?cluster=${i}>\" >> /var/www/index.html"
Share:
16,994
jdorfman
Author by

jdorfman

Hello, my name is Justin Dorfman. I work for MaxCDN as a Developer Advocate. Twitter: @jdorfman

Updated on July 01, 2022

Comments

  • jdorfman
    jdorfman almost 2 years

    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.