如何将多行值分配给 bash 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38090646/
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
How to assign a multiple line value to a bash variable
提问by molecule
I have a variable FOO
with me that needs to be assigned with a value that will be multiple lines. Something like this,
我有一个变量FOO
需要分配一个多行的值。像这样的东西,
FOO="This is line 1
This is line 2
This is line 3"
So when I print the value of FOO
it should give the following output.
所以当我打印FOO
它的值时应该给出以下输出。
echo $FOO
output:
This is line 1
This is line 2
This is line 3
Furthermore, the number of lines will be decided dynamically as I will initialize it using a loop.
此外,行数将动态决定,因为我将使用循环对其进行初始化。
The answers that have been shown in the other question using mainly read -d
is not suitable for me as I am doing intensive string operations and the code format is also important.
在另一个问题 using 中显示的答案主要read -d
不适合我,因为我正在进行密集的字符串操作,并且代码格式也很重要。
回答by John Kugelman
Don't indent the lines or you'll get extra spaces. Use quotes when you expand "$FOO"
to ensure the newlines are preserved.
不要缩进这些行,否则你会得到额外的空格。展开时使用引号"$FOO"
以确保保留换行符。
$ FOO="This is line 1
This is line 2
This is line 3"
$ echo "$FOO"
This is line 1
This is line 2
This is line 3
Another way is to use \n
escape sequences. They're interpreted inside of $'...'
strings.
另一种方法是使用\n
转义序列。它们在$'...'
字符串内部被解释。
$ FOO=$'This is line 1\nThis is line 2\nThis is line 3'
$ echo "$FOO"
A third way is to store the characters \
and n
, and then have echo -e
interpret the escape sequences. It's a subtle difference. The important part is that \n
isn't interpreted inside of regular quotes.
第三种方法是存储字符\
和n
,然后echo -e
解释转义序列。这是一个微妙的区别。重要的部分是它\n
没有在常规引号内解释。
$ FOO='This is line 1\nThis is line 2\nThis is line 3'
$ echo -e "$FOO"
This is line 1
This is line 2
This is line 3
You can see the distinction I'm making if you remove the -e
option and have echo
print the raw string without interpreting anything.
如果您删除该-e
选项并echo
打印原始字符串而不解释任何内容,您可以看到我所做的区别。
$ echo "$FOO"
This is line 1\nThis is line 2\nThis is line 3
回答by Jonny Henly
When you initialize FOO
you should use line breaks: \n
.
初始化FOO
时应使用换行符:\n
.
FOO="This is line 1\nThis is line 2\nThis is line 3"
Then use echo -e
to output FOO
.
然后用于echo -e
输出FOO
。
It is important to note that
\n
inside"..."
is NOT a line break, but literal\
, followed by literaln
. It is only when interpreted byecho -e
that this literal sequence is converted to a newline character. — wise words from mklement0
需要注意的是,
\n
inside"..."
不是换行符,而是literal\
,后跟literaln
。这是仅当由解释echo -e
,这种文字序列被转换成一个新行字符。— 来自mklement0 的明智之言
#!/bin/bash
FOO="This is line 1\nThis is line 2\nThis is line 3"
echo -e $FOO
Output:
This is line 1
This is line 2
This is line 3
回答by Chetabahana
You could also store the lines to a variable using read lines:
您还可以使用读取行将行存储到变量中:
$ read -r -d '' FOO << EOF
This is line 1
This is line 2
This is line 3
EOF
To see the newline on printing use quotes around the variable: (echo "$FOO"
not echo $FOO
)
要查看打印时的换行符,请在变量周围使用引号:(echo "$FOO"
不是echo $FOO
)
$ echo "$FOO"
This is line 1
This is line 2
This is line 3