将多行输出放在变量内 [英] place a multi-line output inside a variable

查看:119
本文介绍了将多行输出放在变量内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用bash编写脚本,我希望它执行命令并分别处理每一行.例如:

I'm writing a script in bash and I want it to execute a command and to handle each line separately. for example:

LINES=$(df)
echo $LINES

它将返回所有将空格换成新行的输出.

it will return all the output converting new lines with spaces.

示例:

如果输出应为:

1
2
3

那我会得到

1 2 3

如何将命令的输出放到允许新行仍然是新行的变量中,所以当我打印变量时,我将获得正确的输出?

how can I place the output of a command into a variable allowing new lines to still be new lines so when I print the variable i will get proper output?

推荐答案

通常在bash中, $ v 会在大多数情况下带来麻烦.几乎总是您真正的意思是双引号中的"$ v" .

Generally in bash $v is asking for trouble in most cases. Almost always what you really mean is "$v" in double quotes.

LINES="`df`"     # double-quote + backtick
echo "$LINES"
OLDPATH="$PATH"

这篇关于将多行输出放在变量内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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