Literal quotes in flags for a command in a bash script

I am writing a Bash script in which I run a perl script that requires double quoted flags. By running the script normally, you will run

$ perl script.pl -flag="something" -anotherflag="somethingelse"

      

In my script, I am using variables in these flags, but would like to keep the double quotes, so I have something like:

variable=foo
perl script.pl -flag=\"something\" -anotherflag=\"$variable\"

      

when i run bash -x myscript.sh

i see that it adds single quotes around the whole flag, keeping double quotes, causing a perl script error:

perl script.pl '-flag="something"' '-anotherflag="foo"'

      

I also tried eval

all of this ( eval perl [...]

) and it removed the double quotes completely. How can I Bash run a script with the double quotes intact?

+2


source to share


1 answer


You speak...:

Running the script normally, you would run

$ perl script.pl -flag = "something" -anotherflag = "somethingelse"

... but then the shell launched at the prompt $

will remove "

: the perl script will never see them (they would just play their part in preventing whitespace, and special characters like '>', '<', '| etc. ., from which they are interpreted - those spaces and / or special characters are simply passed as part of the arg argument). With the values ​​you give them (no spaces, no special sentences), these double quotes are completely useless and there was no difference for them to omit them.



Anyway, if this is indeed the effect you want, just do in your bash script:

perl script.pl -flag="something" -anotherflag="$variable"

      

(or omit the double quotes - it is completely indifferent around something

, indifferent around $variable

, if in your example it is simple foo

, but not in all cases).

0


source







All Articles