user1160199
user1160199

Reputation: 43

Unix print loop outputs on one line

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

Upvotes: 4

Views: 16319

Answers (3)

raghvendra gupta
raghvendra gupta

Reputation: 1

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

Upvotes: 0

jaypal singh
jaypal singh

Reputation: 77085

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

#!/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

Upvotes: 4

John Eipe
John Eipe

Reputation: 11228

Use

echo -n $start

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

Upvotes: 3

Related Questions