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
Try using
printf
instead ofecho
.Support for
-e
withecho
is very shell- and platform-specific, whileprintf
usually just works.(Bash’s builtin command
echo
support-e
, but some other shell’s don’t)Example:
The macOS version of
echo
does not support a-e
switch. In the Linux version ofecho
,-e
asks it to interpret backslash escape sequences, son
is changed to a new-line character. (The bash shell has a builtinecho
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 causestring
to be interpreted as a format string, so%
sequences in it will be interpreted instead of printed.In general you should use
printf
instead ofecho
, 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:Are you perhaps explicitly running the separate program
/bin/echo
? If so, don’t do that; just type the command nameecho
without the path. That way you get theecho
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 notsh
, which is the usual way to get POSIX behavior, but you can still explicitly turn on POSIX mode withset -o posix
. If you’ve done that, you’ll see the same sort ofecho
behavior as the /bin version. You can turn that off withset +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 wayecho
does;echo "$var"
becomesprintf '%sn' "$var"
. But it is consistent.Even in POSIX mode, newer versions of
bash
require thexpg_echo
option to be set forecho
to behave according to the POSIX standard.bash
3.2, the old version that macOS ships, does not require this option.The
-e
option provided bybash
‘s non-standardecho
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 thanecho
.