gpt4 book ai didi

linux - 导出带有特殊字符的 .env

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:02:51 34 4
gpt4 key购买 nike

我正在使用 set -o allexport;来源.env;设置 +o allexport 以从 .env 文件中导出所有变量。不幸的是,一个变量包含 ),因此它会导致语法错误。上面的命令有解决办法吗?我知道可以将它设置在括号中,但是 .env 是在某种程度上自动生成的,没有括号。

例如,Dotenv 看起来像这样:

username=test
password=*fdas_dfar+)mbn

最佳答案

source .env

source 是一个 shell 命令,.env 文件被解析为 shell 文件。您需要引用您的字符串,就像在 shell 中一样。

username=test
password='*fdas_dfar+)mbn'

它的优点是,您可以从该文件运行所有 shell 命令:

username=$(echo test)
password='*fdas_dfar+)mbn'

或者您可以编写自己的解析器,例如:

while IFS='=' read -r name value; do
if [ -z "$value" -o -z "$name" ]; then
echo "Error - unparsable line!" >&2
exit 1
fi

# add quotes around `'`
value=$(<<<"$value" sed "s/'/'\''/g")

# set the variable value using bash special builtin
printf -v "$name" "%s" "$value"

done <.env

关于linux - 导出带有特殊字符的 .env,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55703950/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com