right align/pad numbers in bash
If you are interested in changing width dynamically, you can use printf's '%*s' feature
printf '%*s' 20 hello
which prints
hello
Here's a little bit clearer example:
#!/bin/bash
for i in 21 137 1517
do
printf "...%5d ...\n" "$i"
done
Produces:
... 21 ...
... 137 ...
... 1517 ...
For bash, use the printf
command with alignment flags.
For example:
printf '..%7s..' 'hello'
Prints:
.. hello..
Now, use your discretion for your problem.