4 ms·
The "-n" option was added to UNIX around v6. The "-n" special case opened the floodgates for many more options. And what if I actually wanted to print "-n"?
by mikelward 15y ago
The "-n" option was added to UNIX around v6.
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.
- ilikejam 15y agoSure you can: dave@cronus $ ./echo -n "-n dave@cronus > " -n dave@cronus $ Not pretty, though.
- mikelward 15y agoYou're right. I should have said "no sane way to do it". :-)
- martinp 15y agoOr echo -en "-n\n".
- mturmon 15y agoCute, but does not work with bash builtin echo, for which two -n's equals one -n. bash-3.2$ echo -n -n foo foobash-3.2$ Like always with echo -n, no matter what you try, it's not portable.
- ilikejam 15y agoSo... [dave@mini ~]$ echo -n "-n foo > " -n foo [dave@mini ~]$ Easy!
- bch 15y ago> 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 http://news.ycombinator.com/item?id=2781034
- pyre 15y agoUse the source... ;-) % 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.
- bch 15y agoYour Linux is showing ;). kamloops$ uname -s NetBSD kamloops$ echo $0 sh kamloops$ env POSIXLY_CORRECT=1 echo -n kamloops$
- deleted 15y ago[deleted]
- AlecSchueler 15y ago`echo -e -\\x6E` works in GNU echo and Bash builtin (and likely others).
- bdonlan 15y agoThe printf utility works far better if you want detailed control of your output: printf "%s\n" -n