开发者

How to store and echo multiple lines elegantly in bash? [duplicate]

开发者 https://www.devze.com 2022-12-29 09:23 出处:网络
This question already has answers here: Capturing multiple line output into a Bash variable (7 answers)
This question already has answers here: Capturing multiple line output into a Bash variable (7 answers) 开发者_开发技巧 Closed 5 years ago.

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


echo "$my_var"
0

精彩评论

暂无评论...
验证码 换一张
取 消