26

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?

  • 1
    Why don't use echo It\'s Shell Programming or echo "It's Shell Programming"? – cuonglm Mar 2 '15 at 8:49
  • @cuonglm, because I may use phrase like echo Wow, I'm going to have lots of $$$$$$ now – Zen Mar 2 '15 at 8:51
  • 2
    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 '15 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 '15 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 '15 at 9:06
47

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

  • 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. – Michał Politowski Mar 2 '15 at 10:51
  • 5
    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 '15 at 0:51
1

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
    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 '15 at 3:33
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__

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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