Is it possible to echo the two-character string -n using just the echo command built into the bash shell?

I know I can do printf '%s\n' -n but was just wondering if the echo built-in is capable of outputting the string -n at all.

  • 6
    Covered pretty nicely in unix.stackexchange.com/q/65803/117549 – Jeff Schaller Apr 22 at 15:59
  • I'm not sure what to think about this question. I mean, you already know the practical solution is to use printf, and you probably also know the background about the portability issues with echo in general (the things mentioned in Jeff's link). So, what's left here then? Just an exercise in doing things the hard way, or finding the exact limits of suck in a thing you know sucks? Any result you get is going to be specific some shell or shells anyway, exactly because the behaviour of echo -n is so hideously non-portable. – ilkkachu Apr 23 at 10:19

Using -e and octal 55 for the -:

$ echo -e '\055n'

... or octal 156 for the n, or octal for both:

$ echo -e '\055\0156'

If the -e is bothering you, set the shell option xpg_echo to make echo always interpret backslash sequences (this is not usually what you want though):

$ shopt -s xpg_echo
$ echo '-\0156'

The echo in bash also recognizes hexadecimal:

$ echo -e '\x2d\x6e'

Thanks for all the answers. I'll add one of my own that I've just thought of:

echo -n -;echo n

Put any character before the "-n", and use the "\b" escape sequence in -e mode to erase it in the final output. The below example uses a space for the dummy character:

$ echo -e " \b-n"


$ man echo
       If -e is in effect, the following sequences are recognized:
       \b     backspace

This works visually for printing output to the terminal:

$ echo -e " \b-n" | tee /tmp/test
$ cat /tmp/test

But you may run into problems if you need to parse the output, as it does produce an invisible backspace character:

$ echo -e " \b-n" | wc -c
$ wc -c <<< $(echo -e " \b-n")
$ echo -e "--" | wc -c
  • But with ` \b-n` the output is not the two bytes -n (dash and lowercase letter n). For outputs that are similar-looking but not really the same, you could also use echo "-n ". The trailing white space would be less likely [citation needed] to cause issues than a surprise backspace. – ilkkachu Apr 23 at 10:24

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