How to print "-n" without issuing a newline?
Solution 1
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:
-
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
-
Alternatively, just include a space
$ echo " -n" -n
That, however, adds a space which you probably don't want.
-
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.
-
Use trickery
$ echo n- | rev -n
The
rev
command simply prints its output reversed. -
Use the right tool for the job
$ printf -- '-n\n' -n
Solution 2
Sometimes it's a good idea to use the right tool. You could use printf
instead:
% printf "-n\n"
-n
Solution 3
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.
Solution 4
You guys are really overthinking it.
echo -e \\055n
Or with no trailing newline
echo -en \\055n
Solution 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.
Related videos on Youtube
Star OS
Updated on September 18, 2022Comments
-
Star OS over 1 year
I'm trying to print
-n
using theecho
command. But if i simply typeecho -n
, it only issues a newline, not show up-n
, instead it issues a newline.-
n0rd over 8 yearsDoes
echo -n -- -n
work (don't have any linux box handy to test)? -
Star OS over 8 yearsI'm installing Ubuntu in a virtual machine and it seems to be very slow, wait.
-
n0rd over 8 yearsI've checked - it does not. Probably because it's internal bash command and it's not using
getopt
(???) to parse command line -
Brandin over 8 yearsThis 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"
-
Star OS over 8 years... 100 reputation on all of my communities? Nice reward, moderators!
-
-
Sadi over 8 yearsWov! This is better than mine; can you explain a bit for "beginners" ;-)
-
Sadi over 8 yearsThanks, I've also noticed that it doesn't matter where you add the carriage return, at the beginning or the end.
-
Pilot6 over 8 yearsYou are correct that any `\` valid character will do.
-
terdon over 8 years@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. -
Pilot6 over 8 yearsWell,
printf
is better anyway, but the question is regarding justecho
. -
terdon over 8 years@Sadi yes, that's because adding anything makes it a non-valid option.
-n
is valid,-nfoo
is not so that will be printed. -
Pilot6 over 8 years@terdon
man echo
has it too. Buthelp
is a bit better. -
terdon over 8 yearsI know it has it, it's just not relevant.
/bin/echo
is a completely different thing and when you runecho
in bash, you get the builtin which can be quite different depending on your shell. -
Pilot6 over 8 yearsIt looks like
echo -e "-n\c"
does the exact thing. -
terdon over 8 years@Pilot6 nice! What is
\c
? I don't see it inman ascii
. Also, I just realized thatecho -ne '-n\n'
prints-n\n
and only that, just as desired. -
Pilot6 over 8 years
\c
is suppress further output -
terdon over 8 years@Pilot6 ah, it's an echo-specific thing. Good to know, thanks!
-
Pilot6 over 8 yearsDid you try it? It is wrong.
-
user12205 over 8 years@Pilot6 Did you try it? It is not wrong.
-
Pilot6 over 8 yearsFirst command outputs
-pilot6@Pilot6:~$
. The second outputsn
. -
user12205 over 8 years@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. -
Benubird over 8 yearsYou can also print the extra character, and then erase it:
echo -e "- \x08n"
-
abligh over 8 yearsYup, 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 asecho -e "\055n"
-
123 over 8 years
\b
is backspace,\a
is a bell. -
200_success over 8 yearsThis is a good idea, since there are many variants of
echo
, whileprintf
is better standardized. -
Star OS over 8 years-en is not what i want.
-
Star OS over 8 yearsI've you're gonna extend an answer, please edit the answer.
-
Star OS over 8 yearsI'll edit it so it becomes much clearer it's for a bash script.
-
Star OS over 8 yearsMost viewers here may only want "-n", not "-n ".
-
Dmitry Grigoryev over 8 yearsI'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.
-
Star OS over 8 yearsAsh? Ash? POKEMON i mean, what?
-
Sergiy Kolodyazhnyy over 8 yearsThis is a preferred idea for portability
-
jiwopene over 8 yearsI ran 'busybox ash'
-
Star OS over 8 yearsSay "Busybox" or "Busybox Ash". Not just "Ash" beacuse people may think of that pokemon character or the ******* ash from volcanoes.
-
Star OS over 8 yearsBetter. alt255alt255
-
Star OS over 8 yearsThis uses trickery. You can print anything with it. For example, this prints "cats": echo -n c ; echo -n a ; echo -n t ; echo s If it was echo 2 ; echo 3, it would output 2 in line 1 and 3 in line 2. By doing -n to all of these except the last it would bring all the characters to line 2. So -(line1)n(line2) becomes (line1)-n(line2)
-
muru over 8 years@StarOS
ash
is the name of the Almquist Shell. -
Star OS over 8 yearsJust some explanation about how it works ^^^