I'm echoing some text in a bash script with a variable in it, and want to pad that variable so it will always have the appropriate ammount of spaces to the right to keep the rest of the text aligned.
Here's an example of what I want:
Echoing random number 1080 [ OK ]
Echoing random number 443 [ OK ]
Echoing random number 34842 [ OK ]
The numerical value would be of varying length (probably no longer than 5 or 6 digits).
I know that printf can do this and right align the variable by doing the following:
printf "Echoing random number %5s [ OK ]" $RAND_NUM
However, this would format the text like this:
Echoing random number 1080 [ OK ]
Echoing random number 443 [ OK ]
Echoing random number 34842 [ OK ]
And of course just echoing with spaces doens't work:
echo "Echoing random number ${RAND_NUM} [ OK ]"
Produces this:
Echoing random number 1080 [ OK ]
Echoing random number 443 [ OK ]
Echoing random number 34842 [ OK ]
Is there a way to print the text like my first example?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…