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...
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?
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"
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.
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"
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
~
enable -n echo
echo -n "Some string..."
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.
I had the same issue in IBM z/OS, so I used print instead of echo, and it worked.
print -n "Some string ...."