1

In a for loop, I am using the command:

echo " Job_time is $i : Stopping the job "

Sample output:

Job_time is 6 : Stopping the job
Job_time is 6.50 : Stopping the job

Desired output:

Job_time is 6    : Stopping the job
Job_time is 6.50 : Stopping the job
4
  • Please correct the appearance of your desired output. – Julie Pelletier May 6 '16 at 5:37
  • 4
    one word: printf – cas May 6 '16 at 5:54
  • @cas, you should write that as an answer! – James Youngman May 6 '16 at 7:01
  • @JamesYoungman - feel free to steal my comment and turn it into an answer. i'll upvote it if it's good. comments are free for the taking :) – cas May 6 '16 at 7:37
3

Rather than echo, you can use printf for specifying the width of some field. For the given example, replace

echo " Job_time is $i : Stopping the job "

with

printf ' Job_time is %-4s : Stopping the job \n' "$i"

since $i is apparently a string with different lengths. If it is always a valid floating-point number, you could use a numeric format and make printf show trailing zeros:

printf ' Job_time is %4.2f : Stopping the job \n' "$i"

e.g.,

Job_time is 6.00 : Stopping the job
Job_time is 6.50 : Stopping the job

Further reading:

-1

Always print stopping the job beginning at column 14

for ((i=0; i<100; i++))
do
    local offset=14
    printf "%s%*s\n" "Job_time is $i" $offset ": Stopping the job"

done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.