I'm trying to run a command in a bash script using a variable as a parameter, but I am failing to correctly escape the quotes. This is what I have have:
vsnames=$(comm -13 --nocheck-order $cur_ha_stat_report $old_ha_stat_report | awk -F\| '{print $1}' | tr '\r\n' ' ')
create event type=0xfff00033 text="$vsnames" mh=$dev_mh &
Where vsnames is a string with words separated by spaces. When I run the script, this is what I get:
+ vsnames='VS1 VS2 '
+ create event type=0xfff00033 'text=VS1 VS2 ' mh=xxx
How to I put the $vsnames variable between quotes? I have tried using \\"
but it didn't work, bash just added a bunch of unwanted single-quotes. Any tips? The command I actually need is: create event type=0xfff00033 text="VS1 VS2 " mh=xxx
With your variable vsnames
being set to the 8-character-string VS1 VS2
(as we can see from the trace), doing a
create event type=0xfff00033 text=\""$vsnames"\" mh=xxx
would set the argv[2] of the create
command to
text="VS1 VS2 "
Explanation: I have enclosed $vsnames
into single quotes, to tell bash that we don't want to do word splitting here. Then I wrapped this between literal \\"
quotes, because you want create
to see these quotes.
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.