From: Eric Blake <eblake@redhat.com>
To: John Marshall <john.marshall@sanger.ac.uk>, dash@vger.kernel.org
Subject: Re: builtin read -r and backslash-char escape sequences
Date: Mon, 3 Aug 2015 11:03:16 -0600 [thread overview]
Message-ID: <55BF9ED4.2020301@redhat.com> (raw)
In-Reply-To: <EDC4419D-6669-4211-9005-8970B5499D9A@sanger.ac.uk>
[-- Attachment #1: Type: text/plain, Size: 1313 bytes --]
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 #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]
next prev parent reply other threads:[~2015-08-03 17:05 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-08-03 16:37 builtin read -r and backslash-char escape sequences John Marshall
2015-08-03 17:03 ` Eric Blake [this message]
2015-08-03 17:03 ` Harald van Dijk
2015-08-04 9:56 ` John Marshall
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=55BF9ED4.2020301@redhat.com \
--to=eblake@redhat.com \
--cc=dash@vger.kernel.org \
--cc=john.marshall@sanger.ac.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.