简体   繁体   English

如何从文本文件中获取供bash脚本使用的变量值?

[英]How to obtain variable values from a text file for use by a bash script?

I have a bash script, and I'd like it to read in the values of variables from a text file. 我有一个bash脚本,我希望它从文本文件中读取变量的值。

I'm thinking that in the text file where the values are stored, I'd use a different line for each variable / value pair and use the equals sign. 我想在存储值的文本文件中,对每个变量/值对使用不同的行,并使用等号。

VARIABLE1NAME=VARIABLE1VALUE
VARIABLE2NAME=VARIABLE2VALUE

I'd then like my bash script to assign the value VARIABLE1VALUE to the variable VARIABLE1NAME , and the same for VARIABLE2VALUE / VARIABLE2NAME . 然后,我希望我的bash脚本将值VARIABLE1VALUE分配给变量VARIABLE1NAME ,对于VARIABLE2VALUE / VARIABLE2NAME也是一样。

Since the syntax of the file is the syntax you would use in the script itself, the source command should do the trick: 由于文件的语法是您将在脚本本身中使用的语法,因此source命令应该可以解决问题:

source text-file-with-assignments.txt

alternately, you can use . 或者,您可以使用. instead of source , but in a case like this, using the full name is more clear. 而不是source ,但是在这种情况下,使用全名会更清楚。

The documentation can be found in the GNU Bash Reference Manual . 该文档可以在《 GNU Bash参考手册》中找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM