简体   繁体   中英

Expansion of bash variable in multiple quotes?

I am trying to perform a cURL command within a bash script to POST to a URI. The command requires that one of the arguments be surrounded by double and single quotes ie '"jsimmons"' In my script however this argument is a variable so the command keeps failing which I believe is because the variable is doing some weird expansion and the command is losing the quotes necessary.

For my current attempt, which doesn't work, the argument looks like, '""$watcher""' as I am trying to expand the variable and place that string within the double and single quotes.

How can I expand my variable properly to fulfill the requirements of the command?

If you have double quotes around your whole command, you can insert single quotes without any trouble but need to escape double quotes.

For example:

$ watcher=jsimmons
$ echo "'\"$watcher\"'"
'"jsimmons"'

你可以逃避周围的' S和" s的\\

\\'\\"$watcher\\"\\'

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