4

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

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

Comments

3

Use

echo -n $start

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

Comments

0

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

2 Comments

Can you explain what your code does, and how it improves on the code in the question.
I agree with @Michelle - please also format your answer and use code formatting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.