'echo' without newline in a shell script

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 WRT 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 linefeedn"

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.

链接地址: http://www.djcxy.com/p/4376.html

上一篇: 如何scp文件夹从远程到本地?

下一篇: 'echo'在shell脚本中没有换行符