The "-n" special case opened the floodgates for many more options. And what if I actually wanted to print "-n"? There's no way to do it.
The "-n" special case opened the floodgates for many more options. And what if I actually wanted to print "-n"? There's no way to do it.
Good point.I first tried "echo \-n", and "echo -- -n". No luck. I get the correct visual effect with "echo - ^Hn" (^H generated with ^V, backspace), but the embedded backspace is still actually part of the output :P "echo - ^Hn | col" strips it. Seems like quite an oversight, really, and is prime example of how bugs sneak into code via features.
edit: ilikejam solved it w/o leaning on other tool. As he said "not pretty", but simpler than what I have: http://news.ycombinator.com/item?id=2781034
% env POSIXLY_CORRECT=1 echo -n
-n
Also note that if your shell is Bash, then your echo is a built-in: % type echo
echo is a shell builtin
That's why 'env' is necessary. kamloops$ uname -s
NetBSD
kamloops$ echo $0
sh
kamloops$ env POSIXLY_CORRECT=1 echo -n
kamloops$dave@cronus $ ./echo -n "-n
dave@cronus > "
-n
dave@cronus $
Not pretty, though.
bash-3.2$ echo -n -n foo
foobash-3.2$
Like always with echo -n, no matter what you try, it's not portable.[dave@mini ~]$ echo -n "-n foo
> "
-n foo
[dave@mini ~]$
Easy!
For similar reasons,
echo "" -n
does not work, etc." ' works fine with the bash built-in. It echo's "-n".