【发布时间】:2018-07-17 02:07:24
【问题描述】:
我在 linux 中工作,并且有一个包含很多单行的配置文件,格式如下:
Variable1=Value1
Variable2=Value2
Variable3=Value3
我需要一些可以在命令行上运行的东西,它会回显相应变量的值。我整天都在玩 sed,但玩得很开心。我不确定这是否是最好的方法。任何帮助都会很棒。
【问题讨论】:
我在 linux 中工作,并且有一个包含很多单行的配置文件,格式如下:
Variable1=Value1
Variable2=Value2
Variable3=Value3
我需要一些可以在命令行上运行的东西,它会回显相应变量的值。我整天都在玩 sed,但玩得很开心。我不确定这是否是最好的方法。任何帮助都会很棒。
【问题讨论】:
$ cat a.sh
Variable1=Value1
Variable2=Value2
Variable3=Value3
$ source a.sh
$ echo "$Variable1"
Value1
注意,source 将覆盖当前 shell 的 Variable1 的值。
【讨论】:
. a.sh
搜索变量名和等号,删除它们,然后打印结果。
$ sed -n '/^Variable1=/{s/^Variable1=//;p}' config.txt
Value1
【讨论】:
sed -n '/^Variable1=/s///p'