construct a string with X number of spaces

I'd like to create a variable with the value of X number of space( no Perl please), printf seems to work, but , in following example,10 spaces becomes 1 space when assinged to a variable, Why? other solutions are welcome.

 
$printf "=%10s=\n"
=          =
$var=$(printf "=%10s=\n")
echo $var
= =

in ksh you can use typeset -L (or -R) to define space padded strings

$ typeset -R10 x=
$ echo "=$x="    
=          =
$ echo =$x=    
= =

when you are passing a value with embedded spaces you always need to double quote the variable, otherwise echo (or any other function/utility) would treat multiple consecutive spaces as a single argument separator.

export the value of IFS as below and try ..

$ export IFS=""
$ x=`printf "=%10s="`
$ echo $x
=          =
$

$var does contain the spaces but they disappear when you echo the variable. Try this:

echo "$var"

try with printf :slight_smile:

printf %s"\n" "$var"