Why would you want to use echo
? Use printf
:
printf -- '-n\n'
Or:
printf '%s\n' -n
With echo
:
echo -n
Will output -n<LF>
in a UNIX conformant echo
.
bash
, dash
, GNU or zsh
echo (in their default configurations on most systems) can do it with:
echo -en '-n\n'
Or (on ASCII-based systems):
echo -e '\055n'
Zsh echo
can do it with:
echo - -n
Or (assuming the bsdecho
option is not enabled):
echo '\u002Dn'
Fish's echo
with:
echo -- -n
Ksh and Zsh's print
can do it with:
print - -n
or:
print -- -n
bash
(but not zsh
), assuming the posix
and xpg_echo
are not both enabled:
echo -n -; echo n
Interestingly, it is impossible to output -n
with echo
alone in a POSIX way (that is, more or less, portably across Unices and Unix-likes), since the behaviour if the first argument is -n
or if any argument contains backslash characters is unspecified.
echo " -n"
– Valentin Bajrami Aug 07 '13 at 16:03