如何遍历所有ENV变量的打印键和值? [英] How to iterate through all the ENV variables printing key and value?

查看:143
本文介绍了如何遍历所有ENV变量的打印键和值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历 env 打印中的变量:

I'd like to iterate through the variables in env printing:

name: ${name} value: ${value}

由于存在多行值,因此仅不能通过换行符进行拆分和迭代操作

Simply splitting by line break and iterating does not work, because of multi-line values, e.g.

SERVER_TLS_SERVER_CRT=-----BEGIN CERTIFICATE-----
foo
-----END CERTIFICATE-----

用例是要解决 Docker限制,该限制限制了通过传递多行变量--env-file.

The use case is to workaround Docker limitation that restricts passing multi-line variables via --env-file.

推荐答案

这是#bash的解决方案.

Here's the solution from #bash.

unset IFS
args=() i=0
for var in $(compgen -e); do
    printf -v 'args[i++]' -e%s=%s "$var" "${!var}"
done

我最初以为输出是想法,因此printf%q是必需的,但仅构建一个参数数组时并非如此,因此可以简化为:

I initially thought the idea was to output, hence printf %q was necessary, but that's not the case when just building an arguments array, so it can be simplified to this:

unset IFS
args=()
for var in $(compgen -e); do
    args+=( "-e$var=${!var}" )
done

这篇关于如何遍历所有ENV变量的打印键和值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆