Can I align variables in a string with echo and bash?
Asked Answered
E

1

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.

Ewing answered 3/3, 2010 at 11:43 Comment(0)
A
22

Use printf:

printf "\rFileName : %20s : %8d of %8d Completed" $filename $index $lines
Achene answered 3/3, 2010 at 11:46 Comment(1)
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.Colloquium

© 2022 - 2024 — McMap. All rights reserved.