bash 在shell中打印具有多行值的变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12468233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:42:43  来源:igfitidea点击:

Print a variable with multi-line value in shell?

bashshellunixenvironment-variables

提问by inger

In Bash (or other shells) how can I print an environment variable which has a multi-line value?

在 Bash(或其他 shell)中,如何打印具有多行值的环境变量?

text='line1
line2'

I know a simple usual echo $textwon't work out of the box. Would some $IFS tweak help?

我知道一个简单的通常echo $text不会开箱即用。一些 $IFS 调整会有帮助吗?

My current workaround is something like ruby -e 'print ENV["text"]'. Can this be done in pure shell? I was wondering if envcommand would take an unresolved var name but it does not seem to.

我目前的解决方法类似于ruby -e 'print ENV["text"]'. 这可以在纯shell中完成吗?我想知道envcommand 是否会采用未解析的 var 名称,但似乎没有。

回答by Ignacio Vazquez-Abrams

Same solution as always.

与往常一样的解决方案。

echo "$text"

回答by sirgeorge

export TEST="A\nB\nC"
echo $TEST

gives output:

给出输出:

A\nB\nC

but:

但:

echo -e $TEST
A
B
C

So, the answer seems to be the '-e' parameter to echo, assuming that I understand your question correctly.

因此,假设我正确理解您的问题,答案似乎是echo的“ -e”参数。