How do you escape apostrophe in single quoted string in bash?

294

Solution 1

In single quotes, no escaping is possible. There is no way how to include a single quote into single quotes. See Quoting in man bash.

Solution 2

In addition to POSIX-supported single- and double-quoting, bash supplies an additional type of quoting to allow a small class of escaped characters (including a single quote) in a quoted string:

$ echo $'\'Hello World\''
'Hello World'

See the QUOTING section in the bash man page, near the end of the section. (Search for "ANSI C".)

Solution 3

Simple example of escaping quotes in shell:

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

It's done by closing already opened one ('), placing escaped one (\') to print, 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 (').

What you did ('\'Hello World\''), is:

  1. Opened 1st apostrophe:'.
  2. Closed right after it \', so the string becomes: '\'.
  3. Hello World is not quotes.
  4. Placed standalone apostrophe (\') without opening it.
  5. Last apostrophe (') is opening string, but there is no closing one which is expected.

So the correct example would be:

$ echo \'Hello World\'
'Hello World'

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

Solution 4

To explain what is happening with your escaped apostrophes, we'll examine your second example (also see single quotes, or strong quotes):

$ echo '\'Hello World\''
>     # expects you to continue input

Here, you've left the quotation hanging, as you've stated. Now trim the end and change it to:

                     v                                v           v
$ echo '\'Hello World     # Echo two strings: '\' and 'Hello World'.
\Hello World         ^

The "Hello World" sub-string wasn't quoted here, but it behaved as if it was strong quoted. Using your example again, trim the end differently this time:

                     vv                                    v (plain apostrophe)
$ echo '\'Hello World\'   # Will echo: '\' and 'Hello World''
\Hello World'        ^^   # Note that the trailing ' char is backslash escaped. 

The "Hello World" sub-string again behaves as if it were strong quoted, with only the added apostrophe (escaped, so no longer a single quote) at the end.

When another single quote is added to the end (your original example) the string is left hanging and waiting for a close-quote.

Share:
294
TFchris
Author by

TFchris

Updated on September 18, 2022

Comments

  • TFchris
    TFchris over 1 year

    I would like to know if it is possible in R to convert a vector of characters to numerical values, and then convert those numerical values back to their original characters?

    For example, my code is:

    data <- c("red","blue","green","red","yellow")
    factor_data <- factor(data)
    num_data <- as.numeric(factor_data)
    print(num_data)
    

    I would get

    3 1 2 3 4
    

    where blue = 1, green = 2, red = 3, yellow = 4

    is there a way I can use num_data and get the corresponding character of colors to it?

    My attempt was the code:

    (factor_data)[num_data]
    

    but that just returned me:

    green red blue  green red
    

    which is not the same as the original vector of colors.

    • math
      math over 11 years
      echo \''Hello World'\'
    • alistaire
      alistaire over 6 years
      as.character(factor_data)? ...or are you looking for something like setNames(as.integer(factor(data)), data)?
    • TFchris
      TFchris over 6 years
      I am trying to use num_data to try and get back the colors. So if I were to have a random vector of integers from 1 to 4, i can change that vector of integers into a vector of corresponding colors
  • Kibet
    Kibet over 11 years
    You're right. The trick is in that line 'A single quote may not occur betweeen single quotes even when preceded by a backslash' So it probably splits it into different parts.
  • zero2cx
    zero2cx over 11 years
    @Colin As soon as a single quote is inside of two other single quotes (but backslashed), the quoted quote isn't a real quote anymore. It is just a char with no special pairing characteristics.
  • choroba
    choroba about 10 years
    @zero2cx: I would say "outside" instead of "inside".
  • bufh
    bufh over 9 years
    @choroba not "totally" true, in bash you can do echo $'\'hello world\''
  • Mikl
    Mikl over 4 years
    It's great! Thx