skip to Main Content

I have a problem with printing signs n. My script is make for bash and is set to posix=yes. When I ran program on Mac the printing to bash is done only by echo "text here". But when I ran it on Ubuntu VM, then there must be echo -e "text here because, if there’s not, then the program does not work correctly.

The problem is, when I try to run script with echo -e in MacOS Bash (not zsh), it will print to terminal -e and call that the is not switcher -e for echo.

Why is it doing, please?

To enter bash in terminal on mac I used command "bash"

4

Answers


  1. Try using printf instead of echo.

    Support for -e with echo is very shell- and platform-specific, while printf usually just works.

    (Bash’s builtin command echo support -e, but some other shell’s don’t)

    Example:

    printf "%b" "foonbar"
    
    Login or Signup to reply.
  2. The macOS version of echo does not support a -e switch. In the Linux version of echo, -e asks it to interpret backslash escape sequences, so n is changed to a new-line character. (The bash shell has a builtin echo command that does support -e, so it is unclear why you are seeing -e echoed. You may be executing a script with a shell other than bash. In that case, start the shell script with #!/bin/bash at the top to ensure it is executed with bash.)

    You can print with backslash escape sequences interpreted by using bash’s printf command with a %b format specification:

    printf "%b" string

    Note: Do not merely use printf string as that will cause string to be interpreted as a format string, so % sequences in it will be interpreted instead of printed.

    Login or Signup to reply.
  3. In general you should use printf instead of echo, since it’s consistent across shells.

    But you must be doing something a bit unusual; out of the box, echo -e works fine in bash on macOS, even in the relatively ancient version 3.2 shipped as /bin/bash:

    bash-3.2$ uname -s
    Darwin
    bash-3.2$ echo -e foo
    foo
    bash-3.2$ echo -e 'foonbar'
    foo
    bar
    

    Are you perhaps explicitly running the separate program /bin/echo? If so, don’t do that; just type the command name echo without the path. That way you get the echo that’s built into the shell; if you ask for the separate program by explicit pathname, that’s what gets run instead.

    Or maybe you’re turning on POSIX mode? You said you were starting the shell with bash and not sh, which is the usual way to get POSIX behavior, but you can still explicitly turn on POSIX mode with set -o posix. If you’ve done that, you’ll see the same sort of echo behavior as the /bin version. You can turn that off with set +o posix.

    If you want consistency regardless of mode, I recommend switching to printf. It works a little differently, most notably in that it takes a format string followed by the values to fill in, and doesn’t automatically append a newline the way echo does; echo "$var" becomes printf '%sn' "$var". But it is consistent.

    Login or Signup to reply.
  4. Even in POSIX mode, newer versions of bash require the xpg_echo option to be set for echo to behave according to the POSIX standard.

    ~ % bash --posix
    bash-5.2$ echo 'foonbar'
    foonbar
    bash-5.2$ shopt -s xpg_echo
    bash-5.2$ echo 'foonbar'
    foo
    bar
    

    bash 3.2, the old version that macOS ships, does not require this option.

    ~ % /bin/bash --posix
    
    The default interactive shell is now zsh.
    To update your account to use zsh, please run `chsh -s /bin/zsh`.
    For more details, please visit https://support.apple.com/kb/HT208050.
    bash-3.2$ echo 'foonbar'
    foo
    bar
    

    The -e option provided by bash‘s non-standard echo implementation is used to explicitly enable POSIX behavior.

    To avoid these portability issues, use printf instead, which historically did not have as many diverging implementations, and is much more portable in practice than echo.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search