How can I align the columns of tables in Bash?

前端 未结 9 2003
一个人的身影
一个人的身影 2020-12-02 08:25

I\'d like to output a table format text. What I tried to do was echo the elements of an array with \'\\t\', but it was misaligned.

My code

for((i=0;i         


        
9条回答
  •  粉色の甜心
    2020-12-02 08:40

    printf is great, but people forget about it.

    $ for num in 1 10 100 1000 10000 100000 1000000; do printf "%10s %s\n" $num "foobar"; done
             1 foobar
            10 foobar
           100 foobar
          1000 foobar
         10000 foobar
        100000 foobar
       1000000 foobar
    
    $ for((i=0;i

    Notice I used %10s for strings. %s is the important part. It tells it to use a string. The 10 in the middle says how many columns it is to be. %d is for numerics (digits).

    man 1 printf for more info.

提交回复
热议问题