使用 shell 脚本将变量导出到 linux

export variable to linux using shell script

我想使用 shell 脚本将变量导出到 linux。我的 shell 股票如下:

export dbHost=server01
export dbName=someName

当我在 运行 脚本之后打印这些变量时,我得到以下输出

"eclare -x dbHost=" server01
declare -x dbName=" someName "

所以它正确地导出了 dbName 而不是 dbHost。如果我只保留第一行

export dbHost=server01

然后在脚本中正确打印 dbHost。 我在这里做错了什么???

when I print these variable after running script

export 的变量将仅在您的脚本运行的 shell 和子 shell 中可见。如果你想在你的脚本终止后看到效果,你必须 source 你的脚本。

可以在超级用户对此 question 的回答中找到解释。

我怀疑你的文件中有 DOS 行结尾,你可以用它来测试:

cat -t file.sh

那将显示如下内容:

export dbHost=server01^M
export dbName=someName^M

解决此问题 运行 dos2unix file.sh