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.

  • +1 for "Of course, this doesn't produce the right results". bash is well on the way to making me mad. – Rolf Mar 30 at 11:59
up vote 68 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
  • 5
    Overkill. In most cases you do not need to use string literal syntax. – fpmurphy1 Feb 5 '12 at 16:35
  • @fpmurphy1 Yes, overkill, in any case. You can always use " and backquote only \" or you can use ' and leave the quote for the single quotes 'single quote '"'"' but that looks ugly'. I would downvote this answer if I had enough reputation! – ikrabbe Jun 21 '15 at 14:04
  • 8
    @ikrabbe "Overkill" is not a legitimate reason to downvote an answer. There are only two legitimate reasons to do so 1) the answer is wrong and/or misleading 2) the answer is plagiarized. If you don't agree that a certain answer is the best one but it doesn't violate the two reasons above, the best course of action is to ignore the answer altogether. You are of course free to do as you choose if you get enough reputation, but I encourage you to be a better netizen. – SiegeX Jun 22 '15 at 7:53
  • @SiegeX ok I see. I really just don't like your answer, but you are right. There is a bash tag in the question, so your answer is correct with respect to bash. There are other shells like dash, ksh that don't support this quoting style. – ikrabbe Jun 22 '15 at 9:49
  • 2
    This should be "normal" string treatment and the other crazy things bash does should be "special" treatment. This was figured out in the 3 decades after bash was created. – Rolf Mar 30 at 12:03

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 preexisting single quote character: ' ==> \'
      In particular, ''aaa'' ==> \''aaa'\'

  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\\\"\""

Simple example of escaping quotes in shell:

$ echo 'abc'\''abc'
abc'abc
$ echo "abc"\""abc"
abc"abc

It's done by finishing already opened one ('), placing escaped one (\'), then opening another one (').

Alternatively:

$ echo 'abc'"'"'abc'
abc'abc
$ echo "abc"'"'"abc"
abc"abc

It's done by finishing already opened one ('), placing quote in another quote ("'"), then opening another one (').

Related: How to escape single-quotes within single-quoted strings? at stackoverflow SE

The accepted answer works for simple (one level) quoting:

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

To get the command presented to work, you need to quote twice.
This script could do all the work:

#!/bin/bash

quote () { 
    local quoted=${1//\'/\'\\\'\'};
    printf "'%s'" "$quoted"
}

read -r line <<-\_line_to_quote_
'single quote phrase' "double quote phrase"
_line_to_quote_

quote "$line"; echo
quote "echo $(quote "$line")"; echo

Execute the script to get:

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

The first line works for simple echo:

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

The second line will work for the double quoted command:

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

echo 'I\'m a student' does not work. But the following works:

echo $'I\'m a student' From the man page of bash:

A single quote may not occur between single quotes, even when preceded by a backslash. .... 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.

  • 3
    This adds nothing to the existing answers. – jasonwryan Jan 3 at 5:55

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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