TEXHIK
TEXHIK

Reputation: 1398

How to give variable as command parameter in double quotes?

I need to execute a command, which takes a parameter, needs to be in double quotes:

cmd --key1 --key2 "some parameter in \"double\" quotes and with {brackets}"

this works good from terminal, but when i use .sh script: mess.txt: some parameter in \"double\" quotes and with {brackets}

nither this

message=$(cat mess.txt)
cmd --key1 --key2 "$message"

nor this

message=$(cat mess.txt)
cmd --key1 --key2 \"$message\"

and nor this

message=$(cat mess.txt)
cmd --key1 --key2 "\"$message\""

works, but executing result of this in terminal works:

message=$(cat mess.txt)
echo "cmd --key1 --key2 \"$message\""

Any ideas, how to execute this from script?

Update: if i put " into file and use just $message, this does not work too.

Upvotes: 2

Views: 98

Answers (1)

UltraInstinct
UltraInstinct

Reputation: 44464

[Converting my comment to answer]

Why do you escape the double-quotes when writing it to the file ? You need not do that. Only reason why you escaped that in the original command was because you already had double-quotes outside.

In my terminal:

➜  Temp  cat temp.txt
some parametr in "double" quotes and with {brackets}
➜  Temp  cat temp.py
import sys

print "\n".join(sys.argv)
➜  Temp  python temp.py "$(cat temp.txt)"
temp.py
some parametr in "double" quotes and with {brackets}

Further, in your original command, you could have used single quotes to do away with double-quotes escaping.

➜  Temp  python temp.py 'some parametr in "double" quotes and with {brackets}'
temp.py
some parametr in "double" quotes and with {brackets}

Upvotes: 1

Related Questions