1

I have some strange problem with a very simple script. I am setting LD_LIBRARY_PATH for a library. When I type this command in terminal:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/some/path
echo $LD_LIBRARY_PATH

Output from echo showed the linker path is correctly added. But when I put the exact same command into a .sh file:

#!bin/bash
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/some/path

After execution, output from echo showed the linker path is not added. The script has been set to executable, and executed from correct directory. Could someone let me know what may have gone wrong? Thank you!

Muye
  • 161

1 Answers1

1

A child (your script) can inherit a variable from the parent shell, but not the other way around.

In other words, you can pass a variable from the shell that you are working in to the script you are running in that shell. However, any variable set in the script will not work in the shell you started your script in.

The variable in your script however is set, but only for the shell that is opened for your script. If you would for example add the line "echo $LD_LIBRARY_PATH" to your script, you will get your expected outcome.

If you want to set a variable for a shell that works every time, put it in your .bashrc file (or .bash_profile for OSX).

Vincent
  • 2,898