"echo -n" prints "-n"

Sh

Sh Problem Overview


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...

Sh Solutions


Solution 1 - Sh

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?

Solution 2 - Sh

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"

Solution 3 - Sh

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.

Solution 4 - Sh

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

Solution 5 - Sh

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.

Solution 6 - Sh

Just for the most popular Linux distribution, Ubuntu and its Bash:

  1. Check which shell are you using. Mostly the below works, else see this:

    echo $0

  2. 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"

Solution 7 - Sh

enable -n echo
echo -n "Some string..."

Solution 8 - Sh

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

 ~

Solution 9 - Sh

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.

Solution 10 - Sh

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.

Solution 11 - Sh

I had the same issue in IBM z/OS, so I used print instead of echo, and it worked.

print -n "Some string ...."

print — Return arguments from the shell

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionqwertzView Question on Stackoverflow
Solution 1 - ShKeith ThompsonView Answer on Stackoverflow
Solution 2 - ShaqnView Answer on Stackoverflow
Solution 3 - ShSonnyView Answer on Stackoverflow
Solution 4 - ShDeepam GuptaView Answer on Stackoverflow
Solution 5 - ShAlexandre StrubeView Answer on Stackoverflow
Solution 6 - ShManohar Reddy PoreddyView Answer on Stackoverflow
Solution 7 - ShRiccardo La MarcaView Answer on Stackoverflow
Solution 8 - ShrɑːdʒɑView Answer on Stackoverflow
Solution 9 - ShDaemon42View Answer on Stackoverflow
Solution 10 - ShAmit SinghView Answer on Stackoverflow
Solution 11 - ShAkhileshView Answer on Stackoverflow