bash Unix 打印循环输出在一行上

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8938471/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:22:03  来源:igfitidea点击:

Unix print loop outputs on one line

bashunix

提问by user1160199

I created this script and I want to print the outputs on one line, how do I do this? This is my script

我创建了这个脚本,我想在一行上打印输出,我该怎么做?这是我的脚本

#!/bin/bash

echo "enter start and stop numbers"

read start stop

while [ $start -lt $stop ]

do

echo $start

start=`expr $start + 1`

done

采纳答案by jaypal singh

Using printfor echo -n. Also, try to use start=$(($start + 1))or start=$[$start + 1]instead of back ticks to increment the variable.

使用printfecho -n。此外,尝试使用start=$(($start + 1))start=$[$start + 1]代替反勾号来增加变量。

#!/bin/bash

echo "enter start and stop numbers"
read start stop
while [ $start -lt $stop ]
do
    printf "%d " $start
    start=$(($start + 1))
done


#!/bin/bash

echo "enter start and stop numbers"
read start stop
while [ $start -lt $stop ]
do
    echo -n "$start "  # Space will ensure output has one space between them
    start=$[$start + 1]
done

回答by John

Use

echo -n $start

Check out: http://ss64.com/bash/echo.html

查看:http: //ss64.com/bash/echo.html

回答by raghvendra gupta

for ((i=1;i<=10;i++)); do echo -n $i; done; echo -e "\n"

对于 ((i=1;i<=10;i++)); 做 echo -n $i; 完毕; 回声 -e "\n"