如何将多行值分配给 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

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

How to assign a multiple line value to a bash variable

bashshell

提问by molecule

I have a variable FOOwith 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 FOOit 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 -dis 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 \nescape 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 -einterpret the escape sequences. It's a subtle difference. The important part is that \nisn'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 -eoption and have echoprint 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 FOOyou should use line breaks: \n.

初始化FOO时应使用换行符:\n.

FOO="This is line 1\nThis is line 2\nThis is line 3"

Then use echo -eto output FOO.

然后用于echo -e输出FOO

It is important to note that \ninside "..."is NOT a line break, but literal \, followed by literal n. It is only when interpreted by echo -ethat this literal sequence is converted to a newline character. — wise words from mklement0

需要注意的是,\ninside"..."不是换行符,而是literal \,后跟literal n。这是仅当由解释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