Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting an environment variable before a command in bash

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

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

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.

As I understand it, the initial line is equivalent to

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

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 ?

like image 208
Olorin Avatar asked Apr 26 '26 05:04

Olorin


1 Answers

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.

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 assuming you aren't running the shell script with . or source.

like image 73
Bryan Oakley Avatar answered Apr 28 '26 13:04

Bryan Oakley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!