博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在shell脚本中没有换行符的'echo'
阅读量:2290 次
发布时间:2019-05-09

本文共 2241 字,大约阅读时间需要 7 分钟。

本文翻译自:

I have a problem with echo in my script: 我的脚本中存在echo问题:

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

#1楼

参考:


#2楼

There are multiple versions of the echo command, with different behaviors. echo命令有多个版本,具有不同的行为。 Apparently the shell used for your script uses a version that doesn't recognize -n . 显然,用于脚本的外壳程序使用的版本无法识别-n

The printf command has much more consistent behavior. printf命令具有更一致的行为。 echo is fine for simple things like echo hello , but I suggest using printf for anything more complicated. echo适用于诸如echo hello类的简单事情,但我建议将printf用于更复杂的事情。

What system are you on, and what shell does your script use? 您在什么系统上,脚本使用什么外壳?


#3楼

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. 请注意,我是从man页获得此信息的。 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. man页还指出,shell可能具有自己的echo版本,并且我不确定bash是否具有自己的版本。


#4楼

bash has a "built-in" command called "echo": bash有一个名为“ echo”的“内置”命令:

$ type echoecho 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): 此外,还有一个“ echo”命令,该命令是适当的可执行文件(即,与解释和执行echo相反,shell派生和execs /bin/echo ):

$ 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. echo的WRT到\\c-n各不相同。 Your best bet is to use printf , which is available on four different *NIX flavors that I looked at: 最好的选择是使用printf ,它在我看过的四种不同的* NIX风格中可用:

$ printf "a line without trailing linefeed"$ printf "a line with trailing linefeed\n"

#5楼

如果您在if命令中与其他命令(例如“ read”)一起使用echo,则它可能会忽略该设置,并且无论如何都会跳到新行。


#6楼

Just for the Ubuntu & it's bash : 仅适用于 Ubuntubash

  1. Check which shell are you using? 检查您使用的是哪个外壳? Mostly below works, else : 大多在下面的作品,否则 :

    echo $0

  2. If above prints bash , then below will work: 如果上面打印bash ,那么下面的将起作用:

    printf "hello with no new line printed at end"

    OR 要么
    echo -n "hello with no new line printed at end"

转载地址:http://kpcnb.baihongyu.com/

你可能感兴趣的文章
51nod1136——欧拉函数
查看>>
poj1284——Primitive Roots(欧拉函数)
查看>>
1~n中所有数的欧拉函数值
查看>>
lightoj1370——Bi-shoe and Phi-shoe(欧拉函数应用)
查看>>
Lightoj1341——Aladdin and the Flying Carpet(算术基本定理)
查看>>
Lightoj1336——Sigma Function(因子和)
查看>>
Lightoj1282——Leading and Trailing(幂取模求前三位)
查看>>
Lightoj1259——Goldbach`s Conjecture(哥德巴赫猜想)
查看>>
hdu1848——Fibonacci again and again(SG函数)
查看>>
hdu5734——Acperience(数学推导)
查看>>
hdu5742——It's All In The Mind(模拟)
查看>>
hdu5744——Keep On Movin(构造回文)
查看>>
hdu5776——sum(抽屉原理)
查看>>
hdu5752——Sqrt Bo(水)
查看>>
hdu5793——A Boring Question(快速幂+逆元)
查看>>
poj1797——Heavy Transportation(最大生成树)
查看>>
hdu5810——Balls and Boxes(数学推导)
查看>>
poj3268——Silver Cow Party(最短路+godv之力)
查看>>
poj1860——Currency Exchange(Eellman-Ford+权值为正的环路)
查看>>
poj3259——Wormholes(Eellman-Ford算法)
查看>>