Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: The "-" and "--" options (was Re: ${var:1:1:=y})



04.02.2015, 22:05, "Peter Stephenson" <p.stephenson@xxxxxxxxxxx>:
> On Wed, 4 Feb 2015 09:10:14 -0800
> Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
>>  Also "echo" does NOT accept "--" in this way, it ONLY acceps a solitary
>>  "-".  I forget why that is.
>
> I think the decision for echo (some years ago) was that it would be
> better to make it consistent with versions of /bin/echo rather than with
> the normal internal interface, since shell scripts were written to the
> external echo interface even if the shell had the builtin.
>
> It seems this argument still works on Linux at least:
>
> % /bin/echo -- foo
> -- foo
>
> so I think we were (probably) right and this is the best consistency
> we're going to get.
>
> However, there were already incompatible BSD / SYSV echoes even then, so
> it was never a full consistency with all possible external command
> variants of echo.
>
> pws

`echo` is only usable as long as the option name does not start with a dash and contains no backslashes:

====== test-echo.zsh ======
emulate -L zsh
setopt rcquotes
for shell in zsh bash tcsh fish posh dash bb mksh ksh  ; do
    eval "${shell}_echo() {
              printf 'echo %s' \"\${(j. .)\${(q)@}}\" | $shell
          }"
done
rcsh_echo() {
    printf 'echo %s' "${(j. .)${(qq)@}}" | rcsh
}
for arg in 'a\nb' 'a\\nb' '-E foo' '-e bar' '- baz' '-- bra' ; do
    typeset -A ECHO_RESULTS
    for echofunc in /bin/echo zsh_echo bash_echo tcsh_echo fish_echo posh_echo dash_echo bb_echo mksh_echo ksh_echo rcsh_echo ; do
        echo_result="S$($echofunc $=arg)E"
        ECHO_RESULTS[$echo_result]+="$echofunc "
    done
    printf '>>> %s:\n' "$arg"
    for k in ${(k)ECHO_RESULTS} ; do
        printf '%s: %s\n' "${(qqqq)k}" "${ECHO_RESULTS[$k]}"
    done
    ECHO_RESULTS=()
done
====== /test-echo.zsh ======

====== output ======
>>> a\nb:
$'Sa\nbE': zsh_echo tcsh_echo posh_echo dash_echo mksh_echo 
$'Sa\\nbE': /bin/echo bash_echo fish_echo bb_echo ksh_echo rcsh_echo 
>>> a\\nb:
$'Sa\\nbE': zsh_echo tcsh_echo posh_echo dash_echo mksh_echo 
$'Sa\\\\nbE': /bin/echo bash_echo fish_echo bb_echo ksh_echo rcsh_echo 
>>> -E foo:
$'S-E fooE': tcsh_echo posh_echo dash_echo ksh_echo rcsh_echo 
$'SfooE': /bin/echo zsh_echo bash_echo fish_echo bb_echo mksh_echo 
>>> -e bar:
$'SbarE': /bin/echo zsh_echo bash_echo fish_echo bb_echo mksh_echo ksh_echo 
$'S-e barE': tcsh_echo posh_echo dash_echo rcsh_echo 
>>> - baz:
$'S- bazE': /bin/echo bash_echo tcsh_echo posh_echo dash_echo bb_echo mksh_echo ksh_echo rcsh_echo 
$'SbazE': zsh_echo fish_echo 
>>> -- bra:
$'S-- braE': /bin/echo zsh_echo bash_echo tcsh_echo fish_echo posh_echo dash_echo bb_echo mksh_echo ksh_echo 
$'SbraE': rcsh_echo 
====== /output ======

I do not know which echo zsh behaviour is compatible, but `echo - baz` only shows `baz` in `fish`. All echo versions except rcsh one (this is by Byron Rakitzis’s Plan 9 rc shell reimplementation) (rcsh name is used in Gentoo to prevent conflict with `rc` executable from openrc initialization system) treat `--` as something they need to output, so this part is highly compatible.



Messages sorted by: Reverse Date, Date, Thread, Author