On ash, dash and bash, when I run
$ echo ab$
it returns
ab$
Is this behavior specified by POSIX or is it just a common convention in POSIX-compliant shells? I couldn't find anything on the POSIX Shell Command Language page that mentions this behavior.
Best Answer
$
does not have a special meaning by itself (tryecho $
), only when combined with other character after it and forming an expansion, e.g.$var
(or${var}
),$(util)
,$((1+2))
.The
$
gets its "special" meaning as defining an expansion in the POSIX standard under the section Token Recognition:So, if
$
does not form an expansion, other parsing rules come into effect:That covers your
ab$
string.In the case of a lone
$
(the "new word" would be the$
by itself):The meaning of the generated word containing a
$
that is not a standard expansion is explicitly defined as unspecified by POSIX.Also note that
$
is the last character in$$
, but that this also happens to be the variable that holds the current shell's PID. Inbash
,!$
may invoke a history expansion (the last argument af the previous command). So in general, no,$
is not without meaning at the end of an unquoted word, but at the end of a word it does at least not denote a standard expansion.