[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: /bin/echo -- $var
From: |
Eric Blake |
Subject: |
Re: /bin/echo -- $var |
Date: |
Wed, 14 Aug 2019 08:10:33 -0500 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.0 |
On 8/14/19 7:01 AM, Harald Dunkel wrote:
> Hi folks,
>
> I just learned by accident that
>
> var="-n"
> /bin/echo -- $var
>
> actually prints
>
> -- -n
Correct. This is the POSIX-mandated behavior.
>
> Shouldn't it be just
>
> -n
No, because 'echo' is one of the few exceptions to the rule, in that
POSIX specifically mandates that it NOT recognize -- as an
end-of-options marker. Instead, POSIX recommends that you use printf
instead of echo if the string you are intending to print may contain \
or start with -.
> ?
>
> Other tools in coreutils use '--' to indicate "stop parsing for
> command line flags", e.g. touch, ls and rm:
>
> % /bin/touch -- -l
> % /bin/ls -- -l
> -l
> % /bin/rm -- -l
> % /bin/rm -- -l
> /bin/rm: cannot remove '-l': No such file or directory
>
> Some common style would be nice here.
It would be nice, but it would be non-compliant to the standards. We
can't change this one.
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3226
Virtualization: qemu.org | libvirt.org
signature.asc
Description: OpenPGP digital signature