使用 lastpass bash 脚本导出值中包含空格的环境变量时,收到“不是有效标识符”

when exporting environment variables with spaces in value using lastpass bash script, receiving `not a valid identifier`

我正在使用 lpass-env,它使用 lpass cli 从我的保管库中拉下一个注释并将它们设置为环境变量。

注释看起来类似于:

VARIABLE_WITH_SPACES="abcd 1234"
VARIABLE_WITHOUT_SPACES=abcd1234

当脚本运行以下命令时:

$(lpass show --notes mynote | awk '{ print "export", [=12=] }')

我得到以下输出:

-bash: export: `1234"': not a valid identifier

所以它不喜欢值中的空格。我该如何解决这个问题?

如果您 cat 一个在某些值中包含空格的文件,也会发生同样的事情:

$(cat file | awk '{ print "export", [=14=] }')
-bash: export: `1234"': not a valid identifier

分词shell扩展只扫描命令替换的结果——命令替换扩展后的引号并不特殊,成为参数的一部分。

您可以(滥用)使用 source 命令并传递进程替换:

. <(lpass show --notes mynote | sed 's/^/export /')

或者作恶:

eval "$(lpass show --notes mynote | sed 's/^/export /')"