【发布时间】:2019-10-31 04:00:57
【问题描述】:
我想打印除某些变量之外的所有环境变量。
因此我在stackoverflow上找到了解决方案:Bash: loop through variables containing pattern in name
while IFS='=' read -r name value; do
if [ ! "$name" = "SOMEVAR" -a ! "$name" = "SOMEOTHERVAR" ]; then
echo "$name=$value"
fi
done < <(env)
但是,如果我的环境变量包含换行符,这将不起作用,例如:
export SOMEVAR="some
text with
newlines"
使用上述解决方案,它将打印:
text with=
newlines=
PWD=/home/...
...
我还尝试使用空终止字符串和多行匹配sed:
env -0 | sed -e '{N; s@SOMEVAR.*\x0@@ ; D}' | tr '\0' '\n'
但这会切断输出并且总是首先打印具有多行内容的变量的最后一行:
newlines
PWD=/home/...
...
有没有办法在打印多行环境变量时跳过它们?
【问题讨论】:
标签: bash loops unix environment-variables multiline