如何将`config.sh`中的变量包含到`a.sh`和`b.sh`中?

bod*_*ydo 1 linux variables bash shell

我已经提交了config.sh文件:

data_dir=/home/olle/investing_data
bin_dir=/home/olle/investing_bin
Run Code Online (Sandbox Code Playgroud)

我想使用$data_dir$bin_dir两个单独的脚本a.shb.sh.

我该怎么做?

我在a.sh和b.sh中试过这个:

sh "config.sh"
Run Code Online (Sandbox Code Playgroud)

但是$data_dir并且$bin_dir是空的.

我也试过这个:

/bin/bash "config.sh"
Run Code Online (Sandbox Code Playgroud)

感谢帮助.

che*_*ner 7

您需要source该文件,而不是执行它.在a.shb.sh:

. config.sh
Run Code Online (Sandbox Code Playgroud)

或者bash,更可读的替代方案

source config.sh
Run Code Online (Sandbox Code Playgroud)