Reputation: 21
For example, I'm writing a script in a .sh file, and I want to echo the phrase "string" without a trailing whitespace/new line, so I should probably use echo -n "string"
like how I would achieve this function in bash or zsh. However, when I run the .sh in bash/zsh with sh
command the .sh file would just print -n string
, with a new line included. How do I print something without trailing new line whilst also using the echo
command?
Upvotes: 2
Views: 3086
Reputation: 531225
bash
's implementation of the echo
command differs wildly from the POSIX specification. When bash
is run as sh
, echo
behaves more like POSIX intends, but your sh
may also not be bash
, but a shell like dash
that adheres more strictly to the POSIX specification, which does not allow for any extensions. POSIX requires -n
to be treated like any other argument, something to be output rather than modifying the behavior of echo
.
Use printf
instead, which has a tighter POSIX specification and is much more portable.
printf 'string'
Upvotes: 1