I have a problem with echo
in my script:
echo -n "Some string..."
prints
-n Some string...
and moves to the next line. In the console it's working correcly without newline:
Some string...
There are multiple versions of the echo
command, with different behaviors. Apparently the shell used for your script uses a version that doesn't recognize -n
.
The printf
command has much more consistent behavior. echo
is fine for simple things like echo hello
, but I suggest using printf
for anything more complicated.
What system are you on, and what shell does your script use?
#!/bin/bash
it worked. Normally I'm working with bash.
printf
command.
Sep 24, 2016 at 20:37
echo
always adds newlines... The workaround is jenky at best.
Sep 26, 2016 at 17:58
%
is your shell prompt. Try this, and watch carefully: printf hello; sleep 5
May 12, 2020 at 21:21
bash
has a "built-in" command called "echo":
$ type echo
echo is a shell builtin
Additionally, there is an "echo" command that is a proper executable (that is, the shell forks and execs /bin/echo
, as opposed to interpreting echo
and executing it):
$ ls -l /bin/echo
-rwxr-xr-x 1 root root 22856 Jul 21 2011 /bin/echo
The behavior of either echo
's with respect to \c
and -n
varies. Your best bet is to use printf
, which is available on four different *NIX flavors that I looked at:
$ printf "a line without trailing linefeed"
$ printf "a line with trailing linefeed\n"
/bin/echo -n
to get the no-newline display. printf
may be a better option, but if you're used to using echo, you can still get the functionality you're expecting that way.
Feb 11, 2021 at 15:52
/bin/bash
and /bin/sh
are both bash (and thus use the builtin), but only the former supports the -n
option. I found a nice explanation of that at apple.stackexchange.com/a/410266/157202 ... sh
runs in POSIX compatibility mode.
Jul 19, 2022 at 14:42
Try with
echo -e "Some string...\c"
It works for me as expected (as I understood from your question).
Note that I got this information from the man
page. The man
page also notes the shell may have its own version of echo
, and I am not sure if bash
has its own version.
echo "some string...\c"
instead. If however you need to support multiple bash variants with wildly differing echo commands you probably are better of using printf or you need to be checking the echo commands capabilities with something like [ -n "$( echo -e )" ] && echo "sh stuff\c" || echo -e "bash stuff\c"
(at least bash supports \c as well, so no need to use the -n option, but you need the -e for bash)
-e
is not needed in zsh
. -n
(no new line) works. Why not use zsh
and leave the other shells?
To achieve this there are basically two methods which I frequently use:
1. Using the cursor escape character (\c
) with echo -e
Example :
for i in {0..10..2}; do
echo -e "$i \c"
done
# 0 2 4 6 8 10
-e
flag enables the Escape characters in the string. \c
brings the Cursor back to the current line.OR
2. Using the printf
command
Example:
for ((i = 0; i < 5; ++i)); do
printf "$i "
done
# 0 1 2 3 4
If you use echo inside an if with other commands, like "read", it might ignore the setting and it will jump to a new line anyway.
Just for the most popular Linux distribution, Ubuntu and its Bash:
Check which shell are you using. Mostly the below works, else see this:
echo $0
If above prints bash
, then the below will work:
printf "hello with no new line printed at end"
Or
echo -n "hello with no new line printed at end"
enable -n echo
echo -n "Some string..."
I believe right now your output prints as below
~ echo -e "String1\nString2"
String1
String2
You can use xargs to get multiline standard output into same line.
~ echo -e "String1\nString2" | xargs
String1 String2
~
Note that /usr/bin/echo and /bin/echo on AIX don't support any arguments, so neither -n nor -e work if using sh or KornShell (ksh
) shells.
C shell and Bash have their own built-in echo which supports -n.
This is relevant, because a lot of shell scripts explicitly use sh or KornShell.
AIX does have /usr/bin/printf, so as suggested in some earlier answers,
$ printf "whatever"
is equivalent to echo -n "whatever"
where -n is supported.
When you go and write your shell script, always use #!/usr/bin/env bash
as the first line.
This shell doesn't omit or manipulate escape sequences.
Example:
echo "This is first \\n line"
prints
This is first \n line.
#!/usr/bin/env bash
is in What is the difference between "#!/usr/bin/env bash" and "#!/usr/bin/bash"?.
Nov 5, 2021 at 14:20
I had the same issue in IBM z/OS, so I used print instead of echo, and it worked.
print -n "Some string ...."
bash
in bourne shell mode, it works fine here. Also, it's somewhat unlikely you're really using a bourne shell as your interactive shell, no?echo -ne "text without new line: "; echo "some text";
#!/usr/bin/env sh
at the top. Very curious since sh and bash both claim they are "GNU bash, version 3.2.57(1)-release (arm64-apple-darwin21)". But I proved it:sh -c "echo -n"
prints-n
whereasbash -c "echo -n"
does not.