4

The shell command may have whitespace and single and double quotes.

How can I escape these quotes to correctly pass the command to a POSIX shell, as in the following?

>dash -c 'command'

The other question pointed as a duplicate and asks about double quotes. One of the answers there is probably going to work - to split the command and use concatenated quotes. For example, if the command were

cd "dir"; ls 'foobar'

then we would transform that into

>dash -c 'cd "dir"; ls '"'foobar'"

This is messy... Isn't there an easier way?

I just want a general procedure, an algorithm that takes on input, a string (to be completely precise, made out of printable ASCII characters from 0 to 127 in the ASCII table), and outputs, the second string. The requirement is that if the first string is executed like this

POSIX_shell>string1

The result is the same as

>POSIX_shell -c string2
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mark Galeck
  • 6,155
  • 1
  • 28
  • 55

1 Answers1

3

First of all, try to avoid messy things as you mentioned. If you have to, keep it simple, and keeping all double quotes inside single quote will not have any problem. All single quotes inside double quotes should not be a problem.

#Example:
echo '"These are double quoted"'
echo "'These are single quoted'"

Third (messy and avoid this if possible), any single quote that you want inside a single quote has to be escaped using multiple quotes.

echo ''"'"'These are all single quoted, but messy'"'"''

Fourth, a double quote inside a double quote should be escaped using a backslash \:

echo "\"These are double quoted\""
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
iamauser
  • 11,119
  • 5
  • 34
  • 52
  • the comment "avoid this if possible" reminds me of the canonical joke "customer calls support engineer and says, I get an error when I do this, support engineer says, don't do it then!" – Mark Galeck Jun 11 '16 at 02:17
  • I don't understand your answer. Are you sure you understand my question? – Mark Galeck Jun 11 '16 at 02:21
  • 1
    And you can `echo 'A single quote '\'' inside a single quote.'`, except that it's a fib — the net result is a single quote, but in `'\''`, the first single quote terminates the single-quoted string, the `\'` embeds a single quote, and the final `'` resumes the single-quoted string. – Jonathan Leffler Jun 11 '16 at 02:29
  • @JonathanLeffler I don't know why, I tried to make my question clear, but apparently I have not succeeded, because nobody seems to understand it, yet I don't know how to make it more clear. I will try and add more text to the question. – Mark Galeck Jun 11 '16 at 03:04
  • 1
    @MarkGaleck: The answers to the duplicate cover both single quotes and double quotes, so I regard it as a decent duplicate — I did check it before using Mjölnir to close it as a duplicate. There are lots of ways to do it; they're all messy, and you get to choose which of the messy ways is least messy in your eyes. – Jonathan Leffler Jun 11 '16 at 03:09
  • @JonathanLeffler I see, OK then, I guess there is no non-messy way. OK thank you. – Mark Galeck Jun 11 '16 at 03:10
  • @MarkGaleck: Given variable definitions: `cmd="dash"; x='cd "dir";'" ls 'foobar'"; y='cd "dir"'; z="ls 'foobar'"`, the following commands all work correctly: `$cmd -c "$x"`, `$cmd -c "$y; $z"`, `$cmd -c 'cd "dir"; ls '"'foobar'"`, `$cmd -c 'cd "dir"; ls '\''foobar'\'`, `$cmd -c "cd \"dir\"; ls 'foobar'"`, `$cmd -c "cd "'"'"dir"'"'"; ls 'foobar'"`, `$cmd -c cd\ \"dir\"\;\ ls\ \'foobar\'`. That last two are pretty dire. There are a number of alternatives that could be written using more backslashes. – Jonathan Leffler Jun 11 '16 at 03:23
  • @MarkGaleck -- Yes, people understand your question. However, quoting in Unix shells is an ugly topic. There are no universal, quick-and-easy fixes. It all depends on so many things, and you often end up with tragedies like `echo \\\\\\"hello\\\\\\"` that no programmer wants to see. It may be time to consider a scripting language instead. – Tim Roberts Aug 21 '23 at 17:44