Is there a way to get bash (version 3.2.57(1), if it matters),
in the mode where it's emulating sh, to implement the -n
option
to the echo
builtin?
Specifically, I'm curious whether there might be one option I could set somewhere, rather than seeking out and rewriting N calls to echo -n
in M different scripts, that had — perhaps only by dumb luck — been working fine for years.
Clearly it's possible for bash's echo
builtin to implement -n
, because in full bash mode, it does.
[Disclaimer: Yes, I know that echo -n
is not portable in the first place, and that printf
is recommended.]
Side note: Commentators have noted that this version of bash is "17 years old". Someone should tell Apple about this — it's the version that came with MacOS Ventura 13.0 on the reasonably new Mac where I'm typing this.
printf
instead. – choroba Jan 31 '24 at 14:41echo -n foo
intosh --posix
(bash 5.2) works beautifully. So, yes, might really depend on version (and yours is 17 years old, so things might have reasonably changed!) – Marcus Müller Jan 31 '24 at 14:46echo
like in @MarcusMüller's answer, or just turn off POSIX emulation withset +o posix
and then runecho
(or makeecho
a wrapper function that does this and then doesset -o posix
again). But we can't say if any of that is going to be useful for you. – muru Jan 31 '24 at 15:23bash -c 'echo -n "Line 1\nAlso Line 1\n"'
or you just use/bin/echo
. – paladin Jan 31 '24 at 15:23/bin/echo
is a decent workaround which I was already considering. I was hoping for an option — and you've given me one!set +o posix
is quite likely exactly what I want. Make that an answer, and I'll accept it! – Steve Summit Jan 31 '24 at 15:28echo -n
in bash mode. For some reason it makes a different choice in sh mode. Is there a way to tell it to make the same choice, to implement-n
in sh mode also? And Stéphane Chazelas has now answered most of that. – Steve Summit Jan 31 '24 at 16:46-n
even in POSIX mode. There's at least./configure --enable-xpg-echo-default
, but that actually setsxpg_echo
, while it's not shown as set in the Mac one. So it might just be that the Macsh
just has some other difference. – ilkkachu Jan 31 '24 at 17:46/bin/sh
is still a version of Bash. – ilkkachu Feb 01 '24 at 10:00