在bash中的命令之前设置环境变量 [英] Setting an environment variable before a command in bash

查看:71
本文介绍了在bash中的命令之前设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在安装MPC库,并且遇到了以下命令行(此后称为初始命令"):

I am working on some installation of the MPC library and I came accross this command line (called "the initial command" afterwards) :

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 的位置以及配置命令的位置

where LD_LIBRARY_PATH is set and where the configure command

../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 的另一种设置和另一条命令.

is executed after. Note that after the initial line, there is another line of the same type, with another setting of LD_LIBRARY_PATH and another command.

据我了解,首行等效于

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文件中,则只需用前三行替换它,对吗?如果没有,我该怎么办?

Am I wrong ? If so, if I want to put the initial command in a .sh file, I only have to replace it by the three previous lines, right ? If not, how could I do it ?

推荐答案

您略有错误.导出使该设置可用于当前Shell中的所有命令和子进程.在命令行上进行设置只能在该命令的持续时间内进行设置.

You are slightly wrong. The export makes the setting available for all commands and subprocesses in the current shell. Setting it on the command line sets it only for the duration of that command.

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

If you are writing a shell script, it's quite normal to set the variable and export it once, so you don't have to do it on each line. The value will only be in effect during the execution of the shell script1. It won't affect the parent process that calls the shell script, only the commands within the shell script. You won't need to unset the value at the end of the shell script.

1 假定您没有使用. source 运行shell脚本.

1 assuming you aren't running the shell script with . or source.

这篇关于在bash中的命令之前设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆