Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store output from printf with formatting in a variable? [duplicate]

I would like to store the output of printf with formatting in a variable, but it strips off the formatting for some reason.

This is the correct output

$ printf "%-40s %8s %9s  %7s" "File system" "Free" "Refquota" "Free %"
File system                                  Free  Refquota   Free 

And now the formatting is gone

$ A=$(printf "%-40s %8s %9s  %7s" "File system" "Free" "Refquota" "Free %")
$ echo $A
File system Free Refquota Free %
like image 466
Jasmine Lognnes Avatar asked Jul 08 '15 13:07

Jasmine Lognnes


1 Answers

echo will print each of it's arguments in order, separated by one space. You are passing a bunch of different arguments to echo.

The simple solution is to quote $A:

A=$(printf "%-40s %8s %9s  %7s" "File system" "Free" "Refquota" "Free %")
echo "$A"
like image 75
John Ledbetter Avatar answered Sep 28 '22 15:09

John Ledbetter