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!

marked as duplicate by steeldriver, muru, Community Apr 3 '15 at 2:29

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • thanks a lot, that's the information I was looking for. I will delete this post. – Muye Apr 3 '15 at 2:26
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).

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