0

I'm trying to write a script that adds some path to LD_CONFIG_PATH in order to not get "blabla.so not found". The problem is that my script is able to modify its own context (via export) but it doesn't affect other shells. I want to set the a variable globally so it persists between reboots and shells.

My current script looks like:

LDPATH='/opt/mqm/lib64'
LD_LIBRARY_PATH="$LDPATH:$LD_LIBRARY_PATH"
ldconfig

which I run like

sudo ./set_my_ld_path.sh
$LD_LIBRARY_PATH 

And it returns nothing (LD_LIBRARY_PATH is empty).

I also tried

LDPATH='/opt/mqm/lib64' env
LD_LIBRARY_PATH="$LDPATH:$LD_LIBRARY_PATH"
ldconfig

but it only writes all my existing variables such as PATH/LANG/HOME/LC_TIME/...

For example, I can run following script in powershell and it does what I'd like to have here:

# setting an environment variable for current and descendant processes, same as bash `export`
$env:OPENCV_DIR = $OPENCV_DIR

# applying it machine-wide, persists between users/shells/reboots
[Environment]::SetEnvironmentVariable("OPENCV_DIR", $env:OPENCV_DIR, [EnvironmentVariableTarget]::Machine)

How could it be done?

  • Try loging in as root with sudo -i, then execute your script normally and echo the LD_LIBRARY_PATH afterwards. My guess is that your changes are done for the root user that is executing the command and that they are lost when sudo ends. – Iskustvo May 11 '18 at 10:45
  • @Iskustvo no, this behavior persists for root user too, I run the command without sudo (as I'm already logged as root) and it gets executed successfully, but I don't see the variable change. – Alex Zhukovskiy May 11 '18 at 10:50
4

First of all, assignments can not have spaces around the =.

Secondly, you can never affect the current environment from a child process (a script).

To run a script that sets an environment variable, and have that change affect the current environment, you will need to source the script using either . (dot), or source in bash.

You may also not source a script through sudo (it's unclear why you are using sudo at all in this case).

This means that you can have a script file like this:

LD_LIBRARY_PATH="/opt/mqm/lib64:$LD_LIBRARY_PATH"

which you then source using either

. ./file

or

source ./file

Setting (and exporting) the variable in your shell startup file for interactive shells (~/.bashrc if you use bash), would set it "globally" (for all processes started from the shell).

  • Sorry for spaces around =, it just a habit to place them automatically, I don't actually have them in my script. source seems to be an option, but it has to be mixed somehow with sudo because of different commands in the script (such as ldconfig) that requires it. And even if I remove all sudos and modify LD_CONFIG_PATH in my parent process it doesn't modifies other consoles. – Alex Zhukovskiy May 11 '18 at 11:10
  • Please, see edit. – Alex Zhukovskiy May 11 '18 at 11:16
  • Well, I ended up with adding source as well as modifying /etc/profile for next runs. – Alex Zhukovskiy May 11 '18 at 11:50
  • 1
    @AlexZhukovskiy See added bit. You may also set it in ~/.bash_profile. Setting it in /etc/profile would affect other users. – Kusalananda May 11 '18 at 11:51
  • I actually have found out it's more convinient to do echo '/opt/mqm/lib64' > /etc/ld.so.conf.d/mqm.conf && ldconfig. Works like a charm. – Alex Zhukovskiy May 11 '18 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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