How do I print some text in bash and pad it with spaces to a certain width?

BashVariablesPrintingFormattingPrintf

Bash Problem Overview


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?

Bash Solutions


Solution 1 - Bash

Use - to left align a field.

printf "Echoing random number %-5s   [ OK ]" $RAND_NUM

Alternatively, if you're on a Red Hat Linux system there are predefined functions that will print out green OK and red FAILED prompts (the ones you see during bootup):

#!/bin/bash

. /etc/init.d/functions

echo -n "Frobbing widget:"
frob_widget && echo_success || echo_failure
echo

Solution 2 - Bash

Collect all your lines in one var or text file then pipe it through column command. So this (my example file /tmp/columns.txt)

Echoing random number 1080 [ OK ]
Echoing random number 44332356 [ OK ]
Echoing random number 34842 [ OK ]
Echoing random number 342 [ OK ]

became this

Echoing  random  number  1080      [  OK  ]
Echoing  random  number  44332356  [  OK  ]
Echoing  random  number  34842     [  OK  ]
Echoing  random  number  342       [  OK  ]

Example command: cat /tmp/columns.txt | column -t

Solution 3 - Bash

To expand on sobi3ch's answer: if you concat the strings with a deliminator (I use tilda (~)), you can then call column with the -s param to split the text at that point.

Apologies for the feline abuse:

foo.txt :

Echoing random number 1080~[ OK ]
Echoing random number 1080~[ OK ]
Echoing random number 1080~[ Failed ]

then :

cat foo.txt | column -s'~'

Echoing random number 1080        [ OK ]
Echoing random number 1080        [ OK ]
Echoing random number 1080        [ Failed ]

Solution 4 - Bash

Simple standard Bash function ($1: string to pad; $2: integer padding-length, positive for left-padding, negative for right-padding):

function pad () { [ "$#" -gt 1 ] && [ -n "$2" ] && printf "%$2.${2#-}s" "$1"; }

Usage examples:

$ echo "!$(pad "foobar" 9)!"
!   foobar!
$ echo "!$(pad "foobar" -9)!"
!foobar   !
$ echo "!$(pad "foobar" 3)!"
!foo!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPHLAKView Question on Stackoverflow
Solution 1 - BashJohn KugelmanView Answer on Stackoverflow
Solution 2 - Bashsobi3chView Answer on Stackoverflow
Solution 3 - BashSteve AlmondView Answer on Stackoverflow
Solution 4 - BashNwDsk VederView Answer on Stackoverflow