Adesh
Adesh

Reputation: 57

I need to wrap a substitute variable in double quotes in shell

I am echoing a string and directing it to a text file and in echoed string I need variable substitution which needs to encoded in double-quotes tried a couple of ways but unable to get it.

PASSWORD = XYZ
echo 'password =' \"'"$PASSWORD"'\" > form.txt
cat form.txt
password ='XYZ' # what I am getting
password ="XYZ" # what I am needed

Upvotes: 1

Views: 536

Answers (2)

AF Blue
AF Blue

Reputation: 23

You can just escape " where you need them to be evaluated as char and omit spaces:

PASSWORD=XYZ
echo 'password ='\""$PASSWORD"\" > form.txt

Upvotes: 2

igr
igr

Reputation: 3499

There are different ways. One can be

PASSWORD=XYZ  
echo "password=\"$PASSWORD\"" > form.txt

P.S. Of course, you know that storing password in file is usually a bad idea...

Upvotes: 3

Related Questions