Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm having trouble with escaping characters in bash. I'd like to escape single and double quotes while running a command under a different user. For the purposes of this question let's say I want to echo the following on the screen:

'single quote phrase' "double quote phrase"

How can I escape all the special chars, if I also need to switch to a different user:

sudo su USER -c "echo \"'single quote phrase' \"double quote phrase\"\""

Of course, this doesn't produce the right result.

share|improve this question

2 Answers 2

up vote 8 down vote accepted

You can use the following string literal syntax:

$ echo $'\'single quote phrase\' "double quote phrase"'
'single quote phrase' "double quote phrase"

From man bash

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:

          \a     alert (bell)
          \b     backspace
          \e
          \E     an escape character
          \f     form feed
          \n     new line
          \r     carriage return
          \t     horizontal tab
          \v     vertical tab
          \\     backslash
          \'     single quote
          \"     double quote
          \nnn   the eight-bit character whose value is the octal value nnn (one to three digits)
          \xHH   the eight-bit character whose value is the hexadecimal value HH (one or two hex digits)
          \cx    a control-x character
share|improve this answer
    
This is the other special character I needed! –  m33lky Feb 5 '12 at 5:31
1  
Overkill. In most cases you do not need to use string literal syntax. –  fpmurphy1 Feb 5 '12 at 16:35

In a POSIX shell, assuming in your string there is no variable, command or history expansion, and there is no newline, follow these basic prescriptions:

  1. To quote a generic string with single quotes, perform the following actions:

    1. Substitute any sequence of non-single-quote characters with the same sequence with added leading and trailing single quotes: aaa ==> 'aaa'

    2. Escape with a backslash every single quote character: ' ==> \'
      In particular, a lone single quote will look like this after escaping: '\''

  2. To quote a generic string with double quotes, perform the following actions:

    1. Add leading and trailing double quotes: aaa ==> "aaa"

    2. Escape with a backslash every double quote character and every backslash character: " ==> \", \ ==> \\

A couple of examples:

''aaa""bbb''ccc\\ddd''  ==>  \'\''aaa""bbb'\'\''ccc\\ddd'\'\'
                        ==>  "''aaa\"\"bbb''ccc\\\\ddd''"

so that you example could be expanded with the following:

#!/bin/sh

echo \''aaa'\'' "bbb"'
echo "'aaa' \"bbb\""

sudo su enzotib -c 'echo \'\'\''aaa'\''\'\'\'' "bbb"'\'
sudo su enzotib -c 'echo "'\''aaa'\'' \"bbb\""'

sudo su enzotib -c "echo \\''aaa'\\'' \"bbb\"'"
sudo su enzotib -c "echo \"'aaa' \\\"bbb\\\"\""
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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