解析 .env 文件的 Bash 命令包含等号 (=) 值
我有一个 .env 文件,
# Some variables
USERNAME=user1
PASSWORD=
# Other variables
URL=https://example.com
TOKEN=muDm8qJ6mqM__YX024dk1RXluPnwd-3mxyt1LwLoI4ISiVPA==
目的
我的目的是创建一行 bash 命令来打印
格式,应
- 为每个变量打印一行,名称和名称之间带有 [space] 价值;
- 忽略评论;
- 忽略没有值的变量;
预期输出
预期输出是,
USERNAME user1
URL https://example.com
TOKEN muDm8qJ6mqM__YX024dk1RXluPnwd-3mxyt1LwLoI4ISiVPA==
当前解决方案
我当前的解决方案如下,
grep -v '^#' .env | grep -v '^[[:space:]]*$' | grep -v '=$' | sed 's/=/ /' | while read -r line; do echo $line; done
实际输出
USERNAME user1
URL https://example.com
它只打印出前两行,最后一行的问题是由 TOKEN
中的等号(=)引起的价值。
需要帮助
任何人都可以帮助我纠正命令吗?如果有更简单的方法来实现目标,也欢迎。
I have a .env
file,
# Some variables
USERNAME=user1
PASSWORD=
# Other variables
URL=https://example.com
TOKEN=muDm8qJ6mqM__YX024dk1RXluPnwd-3mxyt1LwLoI4ISiVPA==
Purpose
My purpose is to create one line bash command to print each non-empty variables in <name> <value>
format, which shall
- Print one line for each variable with [space] between name and
value; - Ignore comments;
- Ignore variables with no value;
Expected output
The expected output is,
USERNAME user1
URL https://example.com
TOKEN muDm8qJ6mqM__YX024dk1RXluPnwd-3mxyt1LwLoI4ISiVPA==
Current Solution
My current solution is as following,
grep -v '^#' .env | grep -v '^[[:space:]]*
Actual output
USERNAME user1
URL https://example.com
It only prints out the first two lines, the issue with last line is caused by the equal (=) signs in TOKEN
value.
Help needed
Anyone can help me to rectify the command? also welcome if there is easier way to achieve the goal.
| grep -v '=
Actual output
It only prints out the first two lines, the issue with last line is caused by the equal (=) signs in TOKEN
value.
Help needed
Anyone can help me to rectify the command? also welcome if there is easier way to achieve the goal.
| sed 's/=/ /' | while read -r line; do echo $line; done
Actual output
It only prints out the first two lines, the issue with last line is caused by the equal (=) signs in TOKEN
value.
Help needed
Anyone can help me to rectify the command? also welcome if there is easier way to achieve the goal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Using
sed
Delete lines that do not match a valid variable ie have no inpurt after
=
, this will also match blank lines and for your example data, lines with comments as there is线上没有有效的变量。最后,替换一个空间中=
平等字符的第一次出现。Using
sed
Delete lines that do not match a valid variable i.e have no inpurt after
=
, this will also match blank lines and for your example data, lines with comments as there is no valid variable on the line. Finally, replace the first occurance of the=
equal character for a space.处理任何任何环境变量文件的唯一方法是实际解释它。下面描述了各种打印变量列表的方法。
各种“环境文件”通常设计为POSIX Shell运行以评估所有变量。这使得在分配到变量时可以调用外部命令,执行计算等。
这样做仅是您相信输入文件。使用以下脚本解析文件可以执行任何命令。
您可以源自文件(indentin命令
。
):在摘要之后,可以评估变量。如果您知道要打印的所有变量,请使用此方法(对于vars
用户名
和password
)如果要打印明确指定的所有变量,请使用此方法。 :
但是该解决方案也不是完美的,因为
.env
文件可以使用不同于var_name = value
不同的构造。分配VAR。使用set
命令打印所有变量。这将打印出绝对所有变量。 (实际上,环境文件应从系统环境VAR继承其变量,因此其中很多。)
sed
用于摆脱平等符号。提防一些危险的变量值。这些变量甚至包含newline字符,背景和其他控制字符。
还有许多不同的实用程序来以使其将它们加载回外壳的方式打印变量。例如:
声明-P
或类型-P
- 打印出声明
命令要声明所有当前设置的 shell 变量导出-p
- 相同,但对于 emoverive 变量(这可能是您想要的东西)
The only way to process any environment variable file is to actually interpret it. Various methods for printing out the variable lists are described below.
Various “environment files” are usually designed to be run by POSIX shell to evaluate all the variables. That makes it possible to call external commands, perform calculations etc. when assigning to the variables.
Do this only is you trust you input file. Any command can be executed by parsing the file using the script below.
You can source the file (builtin command
.
):After you source it, you can evaluate the variables. If you know what all variables you want to print, use this (for vars
USERNAME
andPASSWORD
)If you want to print all the variables that are explicitly specified, use this approach:
But this solution is also not perfect, because the
.env
file can use constructions different fromvar_name=value
to assign the vars. Use theset
command to print all variables.This prints out absolutely all variables. (In reality, the environment file should inherit its variables from the system environment vars, so there is a lot of them.)
The
sed
is used to get rid of the equals signs.Beware of some dangerous variable values. The variables can contain even newline character, backspaces and other control characters.
There is also a lot of various utilities for printing the variable in a way that allow loading them back to the shell. For example:
declare -p
ortypeset -p
— prints outdeclare
commands to declare all the currently set shell variablesexport -p
— the same, but for environment variables (that is probably that thing you want)set
— prints out all shell variables如果文件仅包含注释和简单的
key=value
行,字符串中没有#
字符,则可以使用此管道:或者如果您不想输出空变量,请使用这:
If the file contains only comments and simple
key=value
lines without#
chars in strings, you can use this pipeline:Or if you do not want to output empty variables, use this:
我会做这样的事情
为什么?
source
重用解析逻辑的熟悉命令很为什么不呢?
灵感来自 https://unix.stackexchange.com/a/176874
I would do something like this
why?
source
instead of implementing onewhy not?
sed
or other parsing methodsbash -c
for some reasoninspired by https://unix.stackexchange.com/a/176874