如何通过Shell脚本导出多行环境变量

6

我有一个包含以下内容的env.sh文件:

export MULTI_LINES="line1\nline2\nline3"

然后我使用源代码来运行它。
source env.sh

当我检查环境变量时:

printenv | grep MULTI_LINES

它显示

MULTI_LINES=line1\nline2\nline3

如何将 "\n" 转换为换行符?
1个回答

8
如果使用 bash,请使用以下形式将 \n 解释为换行符:
export MULTI_LINES=$'line1\nline2\nline3'

根据man bash

字符串以$'string'的形式特殊处理。该单词扩展为字符串,其中反斜杠转义字符按照ANSI C标准指定的方式替换。

或者可以在多行中放置值:
export MULTI_LINES="line1
line2
line3"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接