14

I want to do formatting using echo in shell scripting.

Here is a small code snippet which is giving me the problem:

echo -en "\rFileName    :   $filename   :    $index of $lines Completed"

The $filename is a string with varying length, and this is causing problem with formatting in the terminal. How can I overcome this?

Here's what I mean:

FileName :       a800_102 :    6 of 6 Completed
FileName :       ersf_1024    :    56 of 56 Completed

I would like to have a table format when I display it on the terminal.

alexwlchan
  • 5,699
  • 7
  • 38
  • 49
Kiran
  • 8,034
  • 36
  • 110
  • 176

1 Answers1

22

Use printf:

printf "\rFileName : %20s : %8d of %8d Completed" $filename $index $lines
richq
  • 55,548
  • 20
  • 150
  • 144
  • 2
    Note that if this line is in a for loop, you need to add newline symbol "\n" after the word "Completed", or it will print only one line. – CodyChan Jan 19 '18 at 09:22