Padding trailing whitespaces in a string with another character

filler='===================='
string='foo'

printf '%s\n' "$string${filler:${#string}}"

Gives

foo=================

${#string} is the length of the value $string, and ${filler:${#string}} is the substring of $filler from offset ${#string} onwards.

The total width of the output will be that of the maximum width of $filler or $string.

The filler string can, on systems that has jot, be created dynamically using

filler=$( jot -s '' -c 16 '=' '=' )

(for 16 = in a line). GNU systems may use seq:

filler=$( seq -s '=' 1 16 | tr -dc '=' )

Other systems may use Perl or some other faster way of creating the string dynamically.


printf "%.20s:\n\n" "$str========================="

where %.20s is the string truncating format


One way to do it:

printf "====================:\r%s\n\n" 'hello world!!'