bash 如何在bash中优雅地存储和回显多行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2752654/
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 19:15:02  来源:igfitidea点击:

How to store and echo multiple lines elegantly in bash?

linuxbashscripting

提问by EmpireJones

I'm trying to capture a block of text into a variable, with newlines maintained, then echo it.

我试图将一段文本捕获到一个变量中,并保留换行符,然后回显它。

However, the newlines don't seemed to be maintained when I am either capturing the text or displaying it.

但是,当我捕获文本或显示文本时,似乎没有保留换行符。

Any ideas regarding how I can accomplish this?

关于我如何做到这一点的任何想法?

Example:

例子:

#!/bin/bash

read -d '' my_var <<"BLOCK"
this
is
a
test
BLOCK

echo $my_var

Output:

输出:

this is a test

这是一个测试

Desired output:

期望的输出:

this

is

a

test

这个

一种

测试

回答by kennytm

echo "$my_var"