bash 同一行上的回声输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25834902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Echo Output on the Same line
提问by user3624000
I am trying get a output printed on a same line using echo command in bash script. Echo Command is given inside a loop, everytime the loop runs, executes a echo command. But normally, echo will take a new line everytime it executes, but I want the output to be on the same line everytime it executes.
我正在尝试使用 bash 脚本中的 echo 命令在同一行上打印输出。Echo 命令在循环内给出,每次循环运行时,执行一个 echo 命令。但通常情况下,echo 每次执行时都会换一行,但我希望每次执行时输出都在同一行。
This is how my sample small code look:
这是我的示例小代码的外观:
#!/bin/bash
loop=1;
while [ $loop -lt 5 ];
do
echo "-"
let loop=loop+1
done
I also tried with -n option of echo command, but I got no output. I replaced the stmt - echo "-" with echo -n "-".
我也尝试过使用 echo 命令的 -n 选项,但没有输出。我用 echo -n "-" 替换了 stmt - echo "-"。
Desired output is: "-----"
所需的输出是:“-----”
Can some one help me on this
有人可以帮助我吗
回答by Keith Thompson
You said you tried
你说你试过了
echo -n "-"
which should have worked. In fact, I suspect that it did.
这应该有效。事实上,我怀疑确实如此。
The problem may be that you don't have a newline at the very end of your output. With the change to
问题可能是您在输出的最后没有换行符。随着更改为
echo -n "-"
your script should print 5 -
characters with no newline. The output may have been clobbered by the shell prompt printed after your script finishes.
您的脚本应该打印 5 个-
字符,没有换行符。脚本完成后打印的 shell 提示可能会破坏输出。
As a quick test, try adding
作为快速测试,请尝试添加
sleep 5
to the end of your script and see if the -----
appears. After 5 seconds, see what happens when your next shell prompt is printed.
到脚本的末尾,看看是否-----
出现。5 秒后,看看打印下一个 shell 提示时会发生什么。
Try this:
尝试这个:
#!/bin/bash
loop=1;
while [ $loop -lt 5 ]
do
echo -n "-"
let loop=loop+1
done
echo ""
The final echo ""
prints just a newline.
最后echo ""
只打印一个换行符。
Some other things to note:
其他一些注意事项:
Indentation is important, especially for larger and more complex scripts. It shows the structure of your code at a glance and makes it much easier to read.
缩进很重要,尤其是对于更大更复杂的脚本。它可以一目了然地显示代码的结构,并使其更易于阅读。
In the code in your question, you have a space in front of the #!/bin/bash
. The #!
is recognized only at the very beginning of the line. Without a valid #!
line, your script will be executed by /bin/sh
, not by /bin/bash
. It probably doesn't matter in this case, since you're not using any bash-specific features. It may have been an error introduced when you posted the question, but be sure to check your actual script and remove the space if it's there.
在您问题的代码中,#!/bin/bash
. 将#!
只在最开始的行认可。如果没有有效的#!
行,您的脚本将由 执行/bin/sh
,而不是由/bin/bash
。在这种情况下这可能无关紧要,因为您没有使用任何特定于 bash 的功能。您发布问题时可能引入了错误,但请务必检查您的实际脚本并删除空格(如果存在)。
The echo
command exists in a number of different versions. It's built into most shells, and also available as a separate executable /bin/echo
. The different versions can have subtly different behavior in which options they recognize and the syntax of special characters. Support for the -n
option to print a line with no terminating newline is probably almostuniversal.
该echo
命令存在于许多不同的版本中。它内置于大多数 shell 中,也可作为单独的可执行文件使用/bin/echo
。不同的版本在它们识别的选项和特殊字符的语法中可能有微妙的不同行为。支持-n
打印没有终止换行符的行的选项可能几乎是普遍的。
The printf
command (similar to C's printf()
function) also exists in multiple implementations, but its behavior is much more consistent. It's safer to use printf
rather than echo
if you're doing anything more than printing a simple one-line message. Again, it probably won't matter in this case, but it's a good thing to keep in mind.
该printf
命令(类似于 C 的printf()
功能)也存在于多种实现中,但其行为更加一致。它的安全使用printf
,而不是echo
如果你更做任何事情比打印一个简单的单行消息。同样,在这种情况下它可能无关紧要,但记住这是一件好事。
回答by Michael Le Barbier Grünewald
You should use echo
for informational messages only and use printf
for everything else, since it is a much more powerful, flexible and reliabletool. The sequence of statements
您应该echo
仅用于信息性消息并printf
用于其他所有内容,因为它是一种更强大、灵活和可靠的工具。陈述的顺序
printf '-'
printf '-'
printf '-'
printf '-'
printf '-'
actually produce the output -----
.
实际产生输出-----
。
回答by player3
Try the following instead of your current echo statement:
尝试以下而不是您当前的 echo 语句:
echo -e "-/c"
echo -e "-/c"
The -e option allows echo to take delimiters, and the /c stops output (in this case the trailing newline that you don't want).
-e 选项允许 echo 使用分隔符,并且 /c 停止输出(在这种情况下是您不想要的尾随换行符)。