Subject: shell quoting problems
To: None <>
From: David Laight <>
List: tech-userlevel
Date: 11/26/2002 18:11:18
I'm trying to write a littel regression test for shell quoting.
Unfortunately I can't quite decide what the correct answer is.

and later sections for the posix words.

The problems arise when quoting might be nested eg:
1)	echo "$( echo "'x'" )"
2)	echo "` echo "'x'" `"
3)	unset a; echo "${a:-"'b'"}"
4a)	a=ab; echo "${a%"'b'"}"
4b)	a="a'b'"; echo "${a%"'b'"}"
4c)	a=ab; echo "${a%'b'}"
4d)	a="a'b'"; echo "${a%'b'}"
5)	unset a; echo "${a:-"b}"c}"
and compounds of the above.

The 'traditional' behaviour (for 2, 3 and 5, since 1 and 4 are not
traditional constructs) is that quotes don't nest, giving:
Solaris 8 /bin/sh (AT&T sh)
  2)	\x (This is buggy!)
  3)	b
  5)	bc}

Solaris /usr/xpg4/bin/sh (AT&T ksh?) gives:
  1)	'x'
  2)	x
  3)	b
  4a)	a
  4b)	a'b'
  4c)	ab
  4d)	a
  5)	bc}

netbsd sh gives:
  1)	'x'
  2)	'x'
  3)	'b'
  4a)	ab
  4b)	a
  4c)	ab
  4d)	a
  5)	b}c

pdksh gives:
  1)	'x'
  2)	'x'
  3)	'b'
  4a)	ab
  4b)	a
  4c)	a
  4d)	a'b'
  5)	b}c

The only enlightening text in the posix standard is:
For '%', '%%', '#' and '##' "Enclosing the full parameter expansion string
in double-quotes shall not cause the following four varieties of pattern
characters to be quoted, whereas quoting characters within the braces shall
have this effect."

There is an inference that it will have that effect for the other types
of parameter expansion.

Under 'command substitution' is:

"A single-quoted or double-quoted string that begins, but does not
end, within the "`...`" sequence produces undefined results."

From which one could infer that in "`... "abc" ...`" the characters
abc are a quoted string, rather than being outside the ones that
contain the ` characters.

Anyone care to comment on which (if any) of the shells are posix


David Laight: