如何存储从printf的输出与可变格式化? [英] How to store output from printf with formatting in a variable?

查看:204
本文介绍了如何存储从printf的输出与可变格式化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用来存储的printf 的输出与一个变量的格式,但它剥去出于某种原因的格式。

这是正确的输出

  $的printf%-40s%787-8%787-9%7S文件系统自由的refquota,免费%
文件系统空闲的refquota免费

而现在的格式已经一去不复返了。

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


解决方案

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

简单的解决方法是引用 $ 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"

这篇关于如何存储从printf的输出与可变格式化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆