捕获多线输出到一个bash变量 [英] Capturing multiple line output to a bash variable

查看:94
本文介绍了捕获多线输出到一个bash变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个剧本的MyScript,输出如下:

I've got a script 'myscript' that outputs the following:

abc
def
ghi

在另一个脚本,我呼吁:

in another script, I call:

declare RESULT=$(./myscript)

$结果得到值

abc def ghi

有没有存储结果要么换行,或者用\\ n字符的方式,这样我就能把它输出回声-e '?

推荐答案

其实,结果中包含你想要的 - 证明:

Actually, RESULT contains what you want — to demonstrate:

echo "$RESULT"

什么告诉你是什么,你从中获取:

What you show is what you get from:

echo $RESULT


由于在评论中指出的,所不同的是:(1)变量的双引号的版本(回声$结果)preserves内部空间而(2)未加引号的版本(回声$结果) - 所有换行符,制表符,多个空格和 - 正是因为它是在变量psented重新$ p $的价值替换一个空格的一个或多个空格,制表符和换行符每个序列。因此(1)preserves输入变量的形状,而(2)创建了输出的一个潜在的非常长单行'单词'由单位(其中字是的非空白字符序列分离;不必有任何的话任何字母数字)


As noted in the comments, the difference is that (1) the double-quoted version of the variable (echo "$RESULT") preserves internal spacing of the value exactly as it is represented in the variable — newlines, tabs, multiple blanks and all — whereas (2) the unquoted version (echo $RESULT) replaces each sequence of one or more blanks, tabs and newlines with a single space. Thus (1) preserves the shape of the input variable, whereas (2) creates a potentially very long single line of output with 'words' separated by single spaces (where a 'word' is a sequence of non-whitespace characters; there needn't be any alphanumerics in any of the words).

这篇关于捕获多线输出到一个bash变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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