0

Per the POSIX Shell Command Language Page:

\

The <backslash> shall retain its special meaning as an escape character (see Escape Character (Backslash)) only when followed by one of the following characters when considered special:

$ ` " \ <newline>

I interpret this to mean that \ should only escape one of these five special characters if it is special in its given context.

The man pages for ash, dash, bash and ksh do not contain the clause "when considered special". I take this to mean (and have confirmed through basic experimentation) that \ has the effect of escaping these five characters unconditionally, even when one of these five characters is not special in its given context.

Consider the output of $ printf '%s\n' "\abcde", which is \abcde. This is expected because a is not special in this context (or any context for that matter); therefore the \ is not special and is treated literally.

Now consider the output of $ printf '%s\n' "\\abcde", which is \abcde. This is not the behavior I expect. In my mind, the first backslash in the string \\abcde should also be treated literally, because it is followed by \ in a context where \ is not special. The output I would expect is \\abcde.

Another scenario where I see unexpected behavior is in the output of $ printf '%s\n' "ab\$", which is ab$. In my mind, $ does have a special meaning it this context because it does serve to introduce paramater expansion, commmand substitution or arithmetic expansion. Therefore, in my mind, the \ should be treated literally because it is followed by $ in a context where $ is not special. The output I would expect is ab\$.

For the record, I hope I am totally off base here. It makes my life that much harder having to worry about escaping these five characters only in certain circumstances. I strive to be posixly correct wherever possible, and I don't want to depend on \ escaping these five special characters unconditionally if this (personally desired) behavior in mainstream POSIX-compliant shells does not fall in line with POSIX.

  • Just use single quotes and you won't have to worry about any of it. – Michael Homer Jan 9 at 4:36
  • @MichaelHomer If I am onto something that is what I will have to do. Just hope I am off base because otherwise, I will have to rewrite all of my scripts in a very ugly, less readable fashion to be on the safe side. – Harold Fischer Jan 9 at 4:39
  • 1
    By using echo, you are investigating how that utility is treating strings, not how the shell is interpreting escaped characters. On my system echo "\abcd" outputs bcd. – Kusalananda Jan 9 at 8:31
  • @Kusalananda, echo '\abcd' outputs a BEL character (0x7 in ASCII) followed by bcd followed by a LF character. – Stéphane Chazelas Jan 9 at 12:40
1

This note makes a recommendation to remove the text when considered special.

A suggested set of corrections is in Note: 0000998.

The note also reports that the change has been approved on 30 Aug 2012.

I am unable to say why is it still used in the present wording.

I believe that you could forget about those words.

0

There are two steps involved in echo "\abc\$\\" (for example).

First, since the string argument to echo is double-quoted, the shell will do things to the escaped characters within.

In the case of "\abc\$\\" it will transform it to \abc$\ before handing it to echo. The \a will remain unaltered since a is not one of the "special" characters. The other escaped characters will be literal, meaning they don't any longer have their special meaning in the shell (the $ does not start an expansion, the backslash does not escape the next character etc.) Note that an $ at the end of a double quoted string is still "special" as it could have introduced an expansion, had the string not ended right after it. The two strings "a$" and "a\$" are identical, as the $ in the first string is not applied to anything.

Then, echo may do its own interpretation of that string argument. Some implementations of echo would consider \a as meaning "the bell character", for example.

I also think you are misreading the standard here. The evaluation of backslash followed by one of the special characters will remove the backslash. You seem to think that it should remain unaltered and that e.g. \$ should be left as \$. The standard says the opposite of that.

  • 1
    The bolded text is about line continuation and clearly not relevant. – Michael Homer Jan 9 at 18:17
  • The question is about whether “when considered special” is restrictive of the “characters”: does the backslash do anything (retain its special meaning) before another backslash that is not “considered special” because it doesn’t precede one of the listed characters and so did not retain its own special meaning. The answer doesn’t seem to address that at all. The whole echo part seems like a (fair) comment on the experimental protocol. – Michael Homer Jan 9 at 18:28

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.