Double quotes in string

Hi all
I have following shell statement:

gs -q -dNODISPLAY -c "(/Users/developer/Desktop/1449367569_Concurrency.pdf) (r) file runpdfbegin pdfpagecount = quit"

and want to execute in elixir the system.cmd function. I tried as follow:

System.cmd "gs", ["-q", "-dNODISPLAY", "-c "(/Users/developer/Desktop/1449367569_Concurrency.pdf) (r) file runpdfbegin pdfpagecount = quit""]

the compiler complain because of " sign. How can I solve it?


I tried to solve the problem with

System.cmd("gs", ["-q", "-dNODISPLAY", "-c \"(/Users/developer/Desktop/1449367569_Concurrency.pdf) (r) file runpdfbegin pdfpagecount = quit\""])

But it does not work at all.

System.cmd("gs", ["-q", "-d", "NODISPLAY", "-c", "(/Users/developer/Desktop/1449367569_Concurrency.pdf) (r) file runpdfbegin pdfpagecount = quit"])

Did you try it without the quotes? System.cmd doesn’t use bash or anything so you don’t need to use "" to indicate that it’s all one argument.

1 Like

I tried the code as you showed but it does not work at all. The statement belongs to each other for example
-dNODISPLAY not space between it.

I tried
System.cmd("gs", ["-q", "-dNODISPLAY", "-c \"(/Users/developer/Desktop/1449367569_Concurrency.pdf) (r) file runpdfbegin pdfpagecount = quit\""])

But is does not work either.

Every flag needs passed in separately from the value associated with the flag.

To give an appropriate answer here, one has to start how bash (and most other shells as well) actually works.

Simplified, the first item you give bash on a line is the program you want to start. Every item after that is a separate argument. Items are divided by spaces. If you want actually pass an argument that contains spaces, you need to either escape the spaces or enclose the item into doublequotes. The called programm will never see the backslashes used to escape or the doublequotes.

In your line you gave us above (gs -q -dNODISPLAY -c "(/Users/developer/Desktop/1449367569_Concurrency.pdf) (r) file runpdfbegin pdfpagecount = quit") bash will call the programm gs with the following arguments (in order):

  • -q
  • -c
  • (/Users/developer/Desktop/1449367569_Concurrency.pdf) (r) file runpdfbegin pdfpagecount = quit

And escatly this is the way, you need to fill the list in System.cmd/2:

System.cmd "gs", ["-q", "-dNODISPLAY", "-c", "(/Users/developer/Desktop/1449367569_Concurrency.pdf) (r) file runpdfbegin pdfpagecount = quit"]

PS: The example System.cmd("gs", ["-q", "-dNODISPLAY", "-c \"(/Users/developer/Desktop/1449367569_Concurrency.pdf) (r) file runpdfbegin pdfpagecount = quit\""]) will pass the following to gs:

  • -q
  • -c "(/Users/developer/Desktop/1449367569_Concurrency.pdf) (r) file runpdfbegin pdfpagecount = quit"

Awesome explaination.