在 bash 中的命令之前设置环境变量

Setting an environment variable before a command in bash

我正在安装 MPC 库,我遇到了这个命令行(之后称为 "the initial command"):

LD_LIBRARY_PATH=/usr/local/gnu/gmp-6.0.0/lib:/usr/local/gnu/mpfr-3.1.2/lib ../configure --prefix=/usr/local/gnu/mpc-1.0.3 --with-gmp=/usr/local/gnu/gmp-6.0.0 --with-mpfr=/usr/local/gnu/mpfr-3.1.2

设置LD_LIBRARY_PATH和配置命令的地方

../configure --prefix=/usr/local/gnu/mpc-1.0.3 --with-gmp=/usr/local/gnu/gmp-6.0.0 --with-mpfr=/usr/local/gnu/mpfr-3.1.2

之后执行。请注意,在初始行之后,还有另一行相同类型,具有另一个设置 LD_LIBRARY_PATH 和另一个命令。

据我了解,第一行相当于

export LD_LIBRARY_PATH=/usr/local/gnu/gmp-6.0.0/lib:/usr/local/gnu/mpfr-3.1.2/lib
../configure --prefix=/usr/local/gnu/mpc-1.0.3 --with-gmp=/usr/local/gnu/gmp-6.0.0 --with-mpfr=/usr/local/gnu/mpfr-3.1.2
unset LD_LIBRARY_PATH

我错了吗?如果是这样,如果我想将初始命令放在 .sh 文件中,我只需要用前三行替换它,对吗?如果没有,我该怎么做?

你有点不对。导出使设置可用于当前 shell 中的所有命令和子进程。在命令行上设置它仅在该命令的持续时间内设置它。

如果您正在编写shell脚本,设置变量并导出一次是很正常的,因此您不必在每一行都进行。该值仅在 shell 脚本 1 执行期间有效。它不会影响调用 shell 脚本的父进程,只会影响 shell 脚本中的命令。您不需要取消设置 shell 脚本末尾的值。

1 假设您不是 运行 具有 .source 的 shell 脚本。