1

I'm trying to pad spaces onto a series of strings in bash, using the printf suggestions outlined here and here.

Iterating over test characters...

for i in a b c d e f g
do
  printf "%-7s" "$i"
done

...produces expected results:

a      b      c      d      e      f      g

But iterating over the actual strings produces strange results:

for i in date sha percent total covered uncovered
do
  printf "%-7s" "$i"
done

date   sha    percenttotal  covereduncovered

Why do those two iterations result in different spacing?

rici
  • 234,347
  • 28
  • 237
  • 341
Scott
  • 98
  • 4
  • 1
    The shell's `printf` command has the useful feature of *repeating* tbe format until it runs out of arguments. So you don't need the`for` loop. You can just do `printf '%-7s' date sha percent total covered uncovered`. – rici Jun 12 '17 at 04:08

2 Answers2

2

Padding only adds spaces to bring the total width up to 7. percent and covered, though, already have 7 characters, so no padding is necessary after either string.

chepner
  • 497,756
  • 71
  • 530
  • 681
0

Is this what you want?

$ for i in date sha percent total covered uncovered; do
>   printf "%-7s%-6s" "$i" ""
> done
date         sha          percent      total        covered      uncovered      $ 

(Notice that my prompt is at the end of that string.)

Jack
  • 5,801
  • 1
  • 15
  • 20