19

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.

6
  • Does echo -n -- -n work (don't have any linux box handy to test)?
    – n0rd
    Nov 23, 2015 at 19:07
  • I'm installing Ubuntu in a virtual machine and it seems to be very slow, wait.
    – Star OS
    Nov 23, 2015 at 19:07
  • 1
    I've checked - it does not. Probably because it's internal bash command and it's not using getopt (???) to parse command line
    – n0rd
    Nov 23, 2015 at 19:46
  • 1
    This question is asked and answered in pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html - - "Conforming applications that ... could possibly be expecting to echo a -n, should use the printf utility derived from the Ninth Edition system"
    – Brandin
    Nov 25, 2015 at 9:42
  • First popular question i have made. Yay?
    – Star OS
    Dec 8, 2015 at 22:05

9 Answers 9

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
    
8
  • You are correct that any `\` valid character will do.
    – Pilot6
    Nov 23, 2015 at 18:08
  • @Pilot6 yeah but the problem with that approach (yours and mine) is that it adds an extra character. That can cause issues down the line. Especially if you then want to parse it and try something like grep '^-n'. pritnf is the way to go, really.
    – terdon
    Nov 23, 2015 at 18:10
  • Well, printf is better anyway, but the question is regarding just echo.
    – Pilot6
    Nov 23, 2015 at 18:11
  • 2
    It looks like echo -e "-n\c" does the exact thing.
    – Pilot6
    Nov 23, 2015 at 18:29
  • 1
    \c is suppress further output
    – Pilot6
    Nov 23, 2015 at 18:45
16

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

% printf "-n\n"       
-n
2
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.

6
  • 1
    Wov! This is better than mine; can you explain a bit for "beginners" ;-)
    – Sadi
    Nov 23, 2015 at 17:59
  • Thanks, I've also noticed that it doesn't matter where you add the carriage return, at the beginning or the end.
    – Sadi
    Nov 23, 2015 at 18:05
  • @Sadi yes, that's because adding anything makes it a non-valid option. -n is valid, -nfoo is not so that will be printed.
    – terdon
    Nov 23, 2015 at 18:20
  • @terdon man echo has it too. But help is a bit better.
    – Pilot6
    Nov 23, 2015 at 18:22
  • I know it has it, it's just not relevant. /bin/echo is a completely different thing and when you run echo in bash, you get the builtin which can be quite different depending on your shell.
    – terdon
    Nov 23, 2015 at 18:23
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.

1
  • Most viewers here may only want "-n", not "-n ".
    – Star OS
    Dec 8, 2015 at 14:17
5

You guys are really overthinking it.

 echo -e \\055n

Or with no trailing newline

 echo -en \\055n
3
  • How is printf "-n\n" an overthinking? Nov 24, 2015 at 12:01
  • Yup, I just answered this way. Will delete my answer. This is relatively portable in that it works with built-in echo and /bin/echo at least if invoked as echo -e "\055n"
    – abligh
    Nov 24, 2015 at 14:05
  • -en is not what i want.
    – Star OS
    Dec 5, 2015 at 19:24
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.

2
  • I've you're gonna extend an answer, please edit the answer.
    – Star OS
    Dec 8, 2015 at 14:13
  • I'm not sure it's a good idea to alter someone else's answer to the point when it will have more text from me than from the original author. Dec 8, 2015 at 14:46
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.

7
  • Did you try it? It is wrong.
    – Pilot6
    Nov 23, 2015 at 20:34
  • @Pilot6 Did you try it? It is not wrong.
    – user12205
    Nov 23, 2015 at 21:11
  • First command outputs -pilot6@Pilot6:~$. The second outputs n.
    – Pilot6
    Nov 23, 2015 at 21:13
  • @Pilot6 Depends on whether you're running it in interactive terminal or in a bash script, which isn't specified in the question. OTOH if you need it in the terminal it is simple to just combine the two lines with && - I wouldn't really say this answer is wrong.
    – user12205
    Nov 23, 2015 at 21:16
  • I'll edit it so it becomes much clearer it's for a bash script.
    – Star OS
    Dec 8, 2015 at 14:14
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
0

You can run

printf -n;echo

Tested in Busybox Ash

5
  • Ash? Ash? POKEMON i mean, what?
    – Star OS
    Dec 8, 2015 at 21:07
  • I ran 'busybox ash'
    – jiwopene
    Dec 8, 2015 at 21:19
  • Say "Busybox" or "Busybox Ash". Not just "Ash" beacuse people may think of that pokemon character or the ******* ash from volcanoes.
    – Star OS
    Dec 8, 2015 at 21:22
  • Better. alt255alt255
    – Star OS
    Dec 8, 2015 at 21:25
  • @StarOS ash is the name of the Almquist Shell.
    – muru
    Dec 8, 2015 at 21:51

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.