Re: builtin read -r and backslash-char escape sequences

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 08/03/2015 10:37 AM, John Marshall wrote:
> Problems with one of my scripts appear to have been caused by dash's read -r translating escape sequences (like \t) whereas several other shells read them literally.  For example:
> 
> $ printf '%s' '\a\t\x' > backslashes 
> $ dash -c 'read -r foo < backslashes; echo "$foo"' | cat -t
> ^G^I\x
> $ bash -c 'read -r foo < backslashes; echo "$foo"' | cat -t
> \a\t\x
> $ ksh -c 'read -r foo < backslashes; echo "$foo"' | cat -t
> \a\t\x
> 
> POSIX says of -r, "Do not treat a <backslash> character in any special way. Consider each <backslash> to be part of the input line" [1].  Translating them as escape sequences doesn't appear to be particularly compatible with this, but conceivably the translation is occurring at some other stage.

Bingo. The "some other stage" is your mistaken use of 'echo "$foo"'.
echo is not portable with backslashes.

$ bash -c 'shopt -s xpg_echo; read -r foo < backslashes; echo "$foo"' |
cat -t
^G^I\x
$ dash -c 'read -r foo < backslashes; printf %s\\n "$foo"' | cat -t
\a\t\x

> 
> Is this the intended behaviour?

No bug here except in your usage of echo when you should have been using
printf.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux