45

I'm reading shell tutorial today from http://www.tutorialspoint.com/unix/unix-quoting-mechanisms.htm

In which it mentions:

If a single quote appears within a string to be output, you should not put the whole string within single quotes instead you whould preceed that using a backslash () as follows:

echo 'It\'s Shell Programming'

I tried this on my centos server, it doesn't work, a > prompts out to hint me type more.

I was wondering, since two single quotes transform every special characters into normal characters, which include escape symbol \, but exclude itself, the ',
how should I represent a single single quote 'in a single-quoted phrase?

6
  • 4
    Why don't use echo It\'s Shell Programming or echo "It's Shell Programming"?
    – cuonglm
    Mar 2, 2015 at 8:49
  • @cuonglm, because I may use phrase like echo Wow, I'm going to have lots of $$$$$$ now
    – Zen
    Mar 2, 2015 at 8:51
  • 3
    So you should use double quotes, and escape $, too. Something like echo Wow, I\'m going to have lots of \$\$\$\$\$\$ now or just echo Wow, I\'m going to have lots of '$$$$$$' now
    – cuonglm
    Mar 2, 2015 at 8:53
  • @cuonglm, yes, I can do it, and I know how I can do it. But the tutorial I've been reading seems is confident about that there is a way to escape single quote inside single-quoted phrases, but the given example of the tutorial doesn't work. So I'm wondering someone may know the solution.
    – Zen
    Mar 2, 2015 at 8:56
  • 1
    How about this? echo 'It'"'"'s Shell Programming'. I just divide it as 3 strings, the first and the last surrounded by single quotes, but the middle one have a single quote surrounded by double quotes.
    – jcbermu
    Mar 2, 2015 at 9:06

4 Answers 4

78

The tutorial is wrong.

POSIX says:

A single-quote cannot occur within single-quotes.

Here's some alternatives:

echo $'It\'s Shell Programming'  # ksh, bash, and zsh only, does not expand variables
echo "It's Shell Programming"   # all shells, expands variables
echo 'It'\''s Shell Programming' # all shells, single quote is outside the quotes
echo 'It'"'"'s Shell Programming' # all shells, single quote is inside double quotes

Further reading: Quotes - Greg's Wiki

2
  • 4
    One more: echo 'It''s Shell Programming' - zsh with rc_quotes option set (and the rc shell too) - a pair of single quotes inside single quotes represents a single single quote. Mar 2, 2015 at 10:51
  • 6
    I sometimes use q="'" and qq='"' to make this cleaner. This then allows echo "It${q}s ${qq}Shell Programming${qq}" (curly brackets are needed given trailing letters). POSIX-safe. The outer layer must be double-quoted.
    – Adam Katz
    Mar 3, 2015 at 0:51
1

In case someone else comes along for putting a mix of single and double quotes into a file, this is working too:

cat > its-shell-programing.txt << __EOF__
echo $'It\'s Shell Programming'
echo "It's Shell Programming"
echo 'It'\''s Shell Programming'
echo 'It'"'"'s Shell Programming'
__EOF__

Everything the shell might see as a variable though must be escaped by a backslash:

cat >> its-shell-programing.txt << __EOF__
echo \$It\'s Shell Programming
__EOF__
0

You can use:

sed "s/'"'/&\\&&/g
     s/.*/'"'&'"'/
' <<IN
$arbitrary_value
IN

To safely shell quote a value line per line.

Shell depending, you might also have the option to do:

printf %q\\n "$arbitrary_value"

Though I usually prefer to do:

a=$(alias "a=$arbitrary_value" a); a=${a#*=}

A more manual approach could look like:

sq(){ set \' "$1"
      while case $2 in (*\'*) :;;
      (*) ! RETURN="$1$2'"     ;;esac
      do  set "$1${2%%\'*}'\''" "${2#*\'}"
      done
}

...which is, at least, fork-free.

1
  • 2
    I totally have no idea about what you are talking about, except the phrase printf %q\\n "$arbitrary_value", others are too complicate.
    – Zen
    Mar 3, 2015 at 3:33
-1
echo "He said to me, \"I've seen that.\""

Seems to me to be the simplest way to return the text:

He said to me, "I've seen that."
5
  • Oops, code was deleted. 'code' echo "He said to me, \"I've seen that\"" Jan 19, 2022 at 19:37
  • Welcome to the site, and thank you for your contribution. Please note however that it seems to be basically reiterating alternative 2 of Mikel's answer (literal single quote enclosed in double quoted string). You may want to consider expanding it so that the difference to that answer becomes more visible; otherwise it would be best placed as a comment to that answer (once you have sufficient reputation).
    – AdminBee
    Jan 20, 2022 at 11:18
  • @AdminBee I guess the difference here is that this solution is escaping the internal double quotes. I don't think that's covered in another answer.
    – terdon
    Jan 21, 2022 at 14:52
  • @terdon True, but the original problem is only about single quotes in single-quoted text, and internal double-quotes are never mentioned, so I wonder how this answer adds anything new ...
    – AdminBee
    Jan 21, 2022 at 14:55
  • 1
    Ah right, true @AdminBee.
    – terdon
    Jan 21, 2022 at 15:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .