Stray "-e" output on macOS and possibly elsewhere

Issue #364 resolved
Paul Hargrove created an issue

The echo built-in in the version of bash provided on macOS expands escape sequences (like \n) by default, and does not recognize the -e option (required on more recent bash versions to expand escapes).

This leads to output like the following at the end of a make tests (among many other places):

[...]
Compiling test-uts_hybrid-smp            SUCCESS
Compiling test-view-smp                  SUCCESS
-e
PASSED compiling 15 tests

Comments (9)

  1. Dan Bonachea

    It appears this problem only occurs because our configure starts with #!/bin/sh which means we are defaulting UPCXX_BASH=/bin/sh and thus setting SHELL=/bin/sh in Makefile.

    Unlike Linux (where on common distros /bin/sh is either a symlink to /bin/bash, or a completely different shell like dash), on macOS /bin/sh is a stand-alone executable that appears to be a "crippled" version of bash (probably configured as --enable-minimal-config: "produces a shell with minimal features, close to the historical Bourne shell.") that appears to be intended for better POSIX shell compat and amongst other things doesn't respect echo -e. When invoked as /bin/bash I do not see this problem on macOS, even though it uses the same reported bash version number.

    So the recommended workaround for current releases is to run configure as /bin/bash configure ...

    I think we should consider fixing this defect in configure instead, to prevent other/future problems that may be lurking with using bash's (poorly documented) minimal-config mode. Ideally we'd simply detect bash versions built as --enable-minimal-config and rexec as bash to avoid this problem entirely.

  2. Paul Hargrove reporter

    I can reproduce the substance of Dan's assertion regarding different behavior on (at least) macOS when (two distinct versions of) bash are invoked under different names:

    mojave:~ phargrov$ /bin/sh -c 'echo -e abc'
    -e abc
    mojave:~ phargrov$ /bin/bash -c 'echo -e abc'
    abc
    

    As Dan says, they report the same version. In fact all variables with the BASH prefix are identical. So, seeking identifying differences seems to come back to shopt:

    mojave:~ phargrov$ /bin/bash -c 'shopt' >A
    mojave:~ phargrov$ /bin/sh -c 'shopt' >B
    mojave:~ phargrov$ diff A B
    9c9
    < expand_aliases        off
    ---
    > expand_aliases        on
    34c34
    < xpg_echo              off
    ---
    > xpg_echo              on
    

    Reading the Bash docs for --enable-minimal-config I see many features which clearly are supported. One easy-to demonstrate example of a feature that would NOT be present in such a minimal config:

    --enable-brace-expansion
    Include csh-like brace expansion ( b{a,b}c → bac bbc ). See Brace Expansion, for a complete description.

    mojave:~ phargrov$ /bin/sh -c 'echo b{a,b}c'
    bac bbc
    

    Another is =~ for regexp matching, which is used so extensively in existing code that configure would certainly not run to completion without it.

    Disturbingly, bash documentation lists over 30 Optional Features, but no way I could find to query which are enabled in a running instance.

    So, I don't (any longer) share Dan's concern that we have such a "minimal configuration" shell as /bin/sh on macOS. However, I am still concerned that we don't know (a) what are the distinguishing properties of this shell (presumably more than two shopt defaults) and (b) how to identify this shell from any other (given that one's .bashrc can mess with shopt).

    At this point, the only concrete idea I have is to add the following in configure to enable -e as an option to built in echo :

    if shopt -q xpg_echo; then
      shopt -u xpg_echo
      BASH+=' +O xpg_echo'
    fi
    

    The append to BASH ensures gmake and our scripts will run with a "good" default for echo.

    NOTE: this does not address any other differences which may be lurking in macOS's /bin/sh.

  3. Log in to comment