[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: echo -n

From: Stephane Chazelas
Subject: Re: echo -n
Date: Mon, 6 Feb 2017 13:57:32 +0000
User-agent: Mutt/1.5.24 (2015-08-30)

2017-02-06 09:45:26 +0530, Jyoti B Tenginakai:
> Again I see that this printf we can use. But there are some scenarios where
> the o/p does not exactly match with echo. So  still its good to have a way
> to pirnt -n /-e/-E with echo. Can this be considered as bug and can this be
> fixed?

echo -e '\055n' # on ASCII-based systems

echo -e '-n\n\c'

echo -ne '-n\n'

Output "-n<newline>" with bash's echo and with the default options.

When in Unix conformance mode (shopt -s xpg_echo; set -o posix)

echo -n

outputs -n followed by a newline character, while

echo '-n\c'

outputs it without the newline character.

All the functionality of echo is available in printf. The %b
format directive has especially been introduced (by POSIX) to
cover for the peculiar style of expansion done by echo (note the
need for \055 instead of \55 like everywhere else) so we have no
excuse to keep using echo.

echo_E() {
  local IFS=' '
  printf '%s\n' "$*"

echo_e() {
  local IFS=' '
  printf '%b\n' "$*"

echo_nE() {
  local IFS=' '
  printf %s "$*"

echo_ne() {
  local IFS=' '
  printf %b "$*"

echo is broken. Stop using it.

ksh and zsh introduced a "print" builtin that fixes some of echo
problems (though it still does expansions by default which you
can disable with -r), but bash chose not to implement it, but
does implement the POSIX (though less Unixy in its CLI style)
printf instead.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]