7

I can't figure how to print the string "-n" with the over 30 years-old command echo.

I tried these:

Michaels:Scripts$ echo -n
Michaels:Scripts$ echo "-n"
Michaels:Scripts$ echo -e "-n"
Michaels:Scripts$ echo -- -n
-- -n
Michaels:Script$ echo -- "-n"
-- -n
Michaels:Script$ echo "\-n"
\-n

I use Mac OS X ML. I consider this behaviour very weird and unexpected.

7

If you don't want to use prinf you have a couple of of options, at least according to this SO Q&A, titled: echo “-n” will not print -n?.

This seems to be your best option:

$ echo "x-n" | cut -c 2-
-n

Or some variation:

$ echo -- '-n'|cut -d" " -f2
-n

printf

printf doesn't have this issue:

$ printf "%s\n" -n
-n
  • Could you please explain the second command, esp. the cut-part of it :) – Michael Aug 7 '13 at 16:05
  • 1
    @Michael - sure, it takes the output from echo -- '-n', which is -- -n. cut then splits the output on spaces, and takes the 2nd field, hence the -n. – slm Aug 7 '13 at 16:06
7

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) can do it with:

echo -en '-n\n'

Or (on ASCII-based systems):

echo -e '\055n'

Zsh echo can do it with:

echo - -n

bash (but not zsh):

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.

  • If you have a unicode terminal, you could output \u2011 (or \u2010) which look identical to a -. But that's hardly a solution (and there's no portable way to introduce the unicode characters without knowing the encoding of the terminal emulator). – rici Aug 8 '13 at 5:48
  • I am using ksh, echo -e "\x2d" is printing "\x2d" but octal expression echo -e "\055" is printing the correct "-" . Any thoughts please? Thanks – Forever Learner May 2 '17 at 14:41
  • 1
    @CppLearner, yes echo is not portable. Don't use echo. For Unix compliant echo's echo '\055' would output the 0x2d byte (- in ASCII, but Unix systems are not required to use ASCII as their base charsets, some still use EBCDIC). But the behaviour for echo '\x2d' is unspecified. Similarly printf '\055' is POSIX and portable, printf '\x2d' is neither. – Stéphane Chazelas May 2 '17 at 14:54
  • Thank you so much Stéphane Chazelas for clarification and quick turnaround. – Forever Learner May 2 '17 at 14:55
2

For historical reasons, echo doesn't do the usual kind of argument parsing where any argument that starts with - is an option except that -- signals the end of option. The echo command mostly prints its arguments unchanged, but depending on the unix variant, on the shell and on how the shell is configured, it may interpret some options (-e, -E, -n, -) and may treat backslash+character specially.

There's no portable way to print -n with echo alone. The portable way to print a string without having to worry about special characters is

printf %s -n

or more generally print %s "$somestring". If you want to print a final newline after the string, make that printf '%s\n' -n.

0

Try this, if you need "-n" as a first two symbols:

echo -e '\r-n'

Another one way:

echo -n '-'; echo 'n'
  • This prints an extra CR at the beginning. – Gilles 'SO- stop being evil' Aug 7 '13 at 23:22
  • What kind of term do you use? – stackexchanger Aug 7 '13 at 23:40
  • What kind of what? Shell? Any shell that implements -e will print 4 bytes: 0d 2d 6e 0a (written out in hex). Any shell that doesn't implement -e will print either 2d 65 20 5c 72 2d 6e 0a or 2d 65 20 0d 2d 6e 0a. – Gilles 'SO- stop being evil' Aug 7 '13 at 23:45
  • Or did you mean what kind of terminal? That's irrelevant. The CR may not be apparent if echo is writing to the terminal, but it's there in the output of echo. – Gilles 'SO- stop being evil' Aug 7 '13 at 23:46
  • Yes, term, means terminal. Questioner just want to print, try my variant with bash + Linux. May be MacOs do it wrong. – stackexchanger Aug 7 '13 at 23:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.