I am trying to run a command in shell script which demands its argument to be passed in below format which includes double quotes with string.

command "string"

This string is stored in variable var1.

I tried below methods but, not able to achieve the requirement. Can someone please suggest the way to achieve it. I am using bash shell.

  1. command $var1 is interpreted as

     command string`
  2. command "$var1" interpreted as

     command string
  3. command \"$var1\" interpreted as

    command '"string"'
  4. command '"'$var1'"' is interpreted as:

    command '"string"'
  • Options 3 and 4 should be correct.
    – D. SM
    May 30, 2020 at 2:43
  • If you by "interpreted as" mean "the set -x trace output says..." then know that the trace output of the shell is only for debugging and that it is annotated for visual inspection (only). The output from set -x traces is not generally suitable for input to the shell.
    – Kusalananda
    May 30, 2020 at 10:05
  • I think you're getting confused between syntactic quotes (which are not part of the actual argument to the command, they're just part of the shell syntax that protects the actual argument from unwanted parsing) and literal quotes (which are actually passed to the command). In command "string", the double-quotes are syntactic; they change how string is parsed by the shell, but are not actually passed to the command (and cannot be required by the command). May 30, 2020 at 16:06

2 Answers 2


The solution to your requirement is to use:

command \""$var1"\"

And no, the set -x output is not (exactly) what the command receives.

  • Thanks Isaac.. This is also interpreted as command '"string"'. When I run this command on terminal as command "string" it works fine. But when I run the same command without quotes or some other quotes it fails. And same I am using in shell script in which in debug mode I see those interpreted values and they are failing due to that.
    – Navi
    May 30, 2020 at 8:44
  • @Navi How do you get command '"string"' ? And how that makes the command fail?
    – user232326
    May 30, 2020 at 8:51
  • Thanks for assistance. I think I am getting confused due to some other issue. I will close this query.
    – Navi
    May 31, 2020 at 1:32
  • Thank you, I wasn't getting the \" on the outside of the ", I've always seen the escaped ones on the inside of the plain double-quote. I needed to build a command string to a job queue system (lsf, sge, slurm) in various stages, but required the part that ends up as the command for the queue to run shall be wrapped in double-quotes as it might be a multiword command string that slurm at least needs quotes around. This has evaded me for a while, but your example here got me working finally.
    – billt
    Feb 2 at 23:03

Either you can set variable like below :

$ var='"string"'
$ echo $var


$ var=string
$ echo $var
$ echo "\"$var\""
$ ls "\"$var\""
ls: cannot access "string": No such file or directory

I believe this is what is expected.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .