[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
bug#20553: 'echo -e' does not escape backslash correctly
From: |
Stephane Chazelas |
Subject: |
bug#20553: 'echo -e' does not escape backslash correctly |
Date: |
Tue, 12 May 2015 09:53:31 +0100 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
2015-05-11 17:36:50 -0600, Eric Blake:
> On 05/11/2015 04:14 PM, Pádraig Brady wrote:
>
> >> echo -e "net use z: \\\\srv\\aqs /persistent:no /user:%USERNAME%
> >> $BG_PASSWD\r"
>
> 'echo -e' is non-portable. POSIX recommends that you use printf
> instead, as the POSIX version of echo is supposed to behave as follows:
>
> $ echo -e 'a\nb'
> -e a\nb
[...]
Strictly speaking without XSI (Unix conformance), the behaviour
of that command is unspecified because the arguments contain
backslash.
With XSI, the behaviour is specified but the expected output is:
-e a<newline>b<newline>
> You are relying on non-POSIX behavior for backslash interpolation.
>
> > Note echo is not portable to other systems, and if that's required,
>
> In fact, it's not even portable to bash:
>
> $ shopt -s xpg_echo
>
> tells bash to turn on POSIX rules for echo, invalidating any bash script
> that relies on 'echo -e'.
xpg_echo alone doesn't make bash's echo POSIX compliant. It just means
-e is implicit. It wouln't break scripts that use "echo -e",
just script that use "echo" without -e and expect escape
sequences not to be expanded.
To have a Unix (POSIX+XSI) conformant echo, you need both the
posix (set -o posix) and xpg_echo (shopt -s xpg_echo) options.
Those can also be enabled via the environment (BASHOPTS,
SHELLOPTS, POSIXLY_CORRECT) or at compile time. bash will also
enable "posix" when called as sh.
See
https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo
for more info.
>
> > printf(1) is a better option, though that will have different
> > quoting again due to the % chars etc.
>
> % doesn't need quoting in shell. But yes, printf is more portable.
% needs escaped (with %) in the format argument though, which is
probaby what Pádraig was refering to.
--
Stephane