20

I'm trying to print -n using the echo command. But if i simply type echo -n, it only issues a newline, not show up -n, instead it issues a newline.

Star OS
  • 2,668

9 Answers9

31

The problem is that echo interprets the -n as an argument. On the default bash implementation, that means (from help echo):

  -n  do not append a newline

There are various ways of getting around that:

  1. Make it into something that isn't an option by including another character. For example, tell echo not to print a newline with -n, then tell it to interpret backslash escapes with -e and add the newline explicitly.

    $ echo -ne '-n\n'
    -n
    
  2. Alternatively, just include a space

    $ echo " -n"
     -n
    

    That, however, adds a space which you probably don't want.

  3. Use a non-printing character before it. Here. I am using the backspace (\b)

    $ echo -e "\b-n"
    -n
    

    This also adds an extra character you probably don't want.

  4. Use trickery

    $ echo n- | rev
    -n
    

    The rev command simply prints its output reversed.

  5. Use the right tool for the job

    $ printf -- '-n\n'
    -n
    
terdon
  • 104,119
16

Sometimes it's a good idea to use the right tool. You could use printf instead:

% printf "-n\n"       
-n
A.B.
  • 92,125
7

You can use this command, but it adds an extra space.

echo -e  "\r-n"

This is a kind of a hack.

-e enables backslash command symbols.

\r is a carriage return.

Actually any \ valid character will do in any place of the string.

You can see which are valid by help echo.

echo "-n" does not work because -n is used as a parameter for echo.

P.S. The best solution IMHO is

echo -e "-n\c"

It does not add any extra characters.

echo -e "-n\n"

prints the same but with a new line char.

Pilot6
  • 92,041
5

I think if you definitely want to use echo only, this should satisfy you:

echo "-n "

This works because while -n is a valid option for echo, -n with a space after it is not. Since it isn't an option, echo just prints it.

terdon
  • 104,119
Sadi
  • 11,074
5

You guys are really overthinking it.

 echo -e \\055n

Or with no trailing newline

 echo -en \\055n
Joshua
  • 719
  • 3
  • 9
2

In Bash script you can run:

echo -n -
echo n

Or in interacive shell:

echo -n - ; echo n

This echoes a - character and an n character.

jiwopene
  • 209
2

To extend @A.B's answer, the only portable way to use echo is to refrain from using any options like -n. Consider use printf instead where available. This reference page provides more details and explains very well when and how echo and printf should be used:

Nowadays, echo(1) is only portable if you omit flags and escape sequences. Use printf(1) instead, if you need more than plain text.

Dmitry Grigoryev
  • 1,960
  • 14
  • 23
1

Three other ways:

$ echo -e '\x2dn' # ASCII hexadecimal value
-n
$ echo -e '\u002dn' # Unicode code point
-n
$ echo -e '\u2dn' # Unicode code point shortened
-n
kos
  • 41,268
0

You can run

printf -n;echo

Tested in Busybox Ash

jiwopene
  • 209