This question already has an answer here:

How can I properly escape arbitrary commands?

For example:

sudo -u chris sh -c 'echo "\"leftright\""'

The above echos:


How would I echo out:


I've tried the following which I would expect to work but does not:

sudo -u chris sh -c 'echo "\"left\'right\""'

I can't quite get my head round how it is parsed.

marked as duplicate by Gilles command-line Jul 19 '15 at 22:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


sudo -u chris sh -c 'echo "\"left'\''right\""'

  • Can you explain this? What other characters do I need to potentially escape to allow any arbitrary commands? – Chris Stryczynski Jul 19 '15 at 19:34
  • It's basically how you handle the opening & the closing quote. For your above example you could also write it as : sh -c 'echo \"left"'\''"right\"'. Check this for reference. Also check here for a good explanation by liori – neuron Jul 19 '15 at 19:47
 sh -c 'echo "\"left'"'"'right\""'


 sh -c 'echo "\"left'\''right\""'

will work.

Either of the above will pass

 echo "\"left'right\""

after -c.

When you're inside a single quoted string there is no escaping other than closing that string.

To pass a single quote, you can either use





The shell will concatenate adjacent arguments that don't have an input field separator (usually space) in between.

Not the answer you're looking for? Browse other questions tagged or ask your own question.