且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何存储从printf的输出与可变格式化?

更新时间:2023-11-10 09:19:10

回声将打印的每一个它是为了论点,用一个空格隔开。您传递了一堆不同的参数,以回声

简单的解决方法是引用 $ A

  A = $(的printf%-40s%787-8%787-9%7S文件系统***的refquota,免费%)
回声$ A

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 %

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"