3

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 ?

8
  • It's not really equivalent, in that after the command is executed the variable is not unset, but restored to its previous value. Try it: a=hello; a=goodbye printenv a; echo "$a". Now, in a script, the modifications made to a variable are not seen by the parent shell, so you don't really care about the unset part anyway (provided your script only does that, and that it is executed and not sourced). Commented Sep 27, 2015 at 17:32
  • Oh ok. SO, instead of just doing unset LD_LIBRARY_PATH, I could do it and set LD_LIBRARY_PATH right after to its previous value, value that I would have stored before having modified it. Is there an elegant way of doing this ? What do you mean by "executed and not sourced" ? (I am really interested in the script strict equivalent of the initial command line.)
    – Olórin
    Commented Sep 27, 2015 at 17:36
  • I don't know about elegance. But what you can do is: 1. Not bother about setting back to its original value, since you'll be running a script, and as I said in my previous comment, modifications of variables in script are not seen by parent shell. 2. You do bother (why?) about setting back to its original value: so just wrap it in a subshell: use parentheses: ( export LD_LIBRARY_PATH=...; ./configure ...). 3. Otherwise it's tricky to save the value of the variable and restore it: you also have to save whether it was set or not… Commented Sep 27, 2015 at 17:40
  • Why would you not simply put the "initial command" in the script?
    – tripleee
    Commented Sep 27, 2015 at 17:40
  • 1
    That's because the shell evaluates the command (and thus expands the variable) before running it. Try foo=/usr/bin perl -le 'print $ENV{"foo"}' instead.
    – tripleee
    Commented Sep 27, 2015 at 17:50

1 Answer 1

2

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.

4
  • So, imagine I am doing two command lines : foo=/usr/bin cmd1 and then foo=/tmp/foo cmd2 where foo is an environment variable having already a value, let's say /usr/local/lib, before the execution of the two command. After each command echoing foo would print /usr/local/lib. (Just tested it in a terminal.) Now, how could I reproduce the two command lines (and foo's behaviour wrt them) in an file.sh file, that I'd run as follows : ./file.sh ? (As you may guess, I don't want other commands executed in the sh file to be perturbated by bad values of foo ...
    – Olórin
    Commented Sep 27, 2015 at 17:59
  • Anything you can do on the command line can be put in a script, and vice versa.
    – tripleee
    Commented Sep 27, 2015 at 18:01
  • @tripleee I don't know that for sure, but if it is true, the command line cannot be pasted as it is in the script (foo=/usr/bin echo ${foo}...). I understand it can be done, but I don't want to resort to perl (as you showed in a comment to my question) for now, as I am fairly new to *nix word -- I want to keep it simple.
    – Olórin
    Commented Sep 27, 2015 at 18:03
  • @user10000100_u: you would do it in a script exactly the same way as a terminal. There is absolutely no difference between the two. Commented Sep 27, 2015 at 19:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.