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

时间:2022-09-27 23:11:50

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 $text won'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 env command would take an unresolved var name but it does not seem to.

我目前的解决方法就像ruby -e'print ENV [“text”]'。这可以用纯壳完成吗?我想知道env命令是否会采用未解析的var名称,但似乎不是。

2 个解决方案

#1


33  

Same solution as always.

和往常一样的解决方案

echo "$text"

#2


3  

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.

所以,答案似乎是回声的'-e'参数,假设我正确理解你的问题。

#1


33  

Same solution as always.

和往常一样的解决方案

echo "$text"

#2


3  

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.

所以,答案似乎是回声的'-e'参数,假设我正确理解你的问题。