2

In order to get some C code to compile correctly, I need to export a folder so that are certain file is found.

On the command line, if I first run

export LD_LIBRARY_PATH=/bin/path../version/:$LD_LIBRARY_PATH

and then the executable from the C code works. If I do not export LD_LIBRARY_PATH, then the executable will fail, as the C package cannot find the necessary file, i.e.

exectuable: error while loading shared libraries: file1.so: cannot open shared object file: No such file or directory

I would now like to run this code via a bash script. Here is my bash script, run_stuff.sh:

#!/bin/bash

source ~/.bash_profile

export LD_LIBRARY_PATH=/bin/path../version/:$LD_LIBRARY_PATH

path/to/executable/executable 

This fails, with the error above. I have since tried

echo "export LD_LIBRARY_PATH=/bin/path../version/:$LD_LIBRARY_PATH"

within the bash script, and I have added

export LD_LIBRARY_PATH=/bin/path../version/:$LD_LIBRARY_PATH

within ~/.bash_profile. It still doesn't work---the C code cannot find this file.

How does one correctly export a folder within a bash script?

1
  • 1
    I would recommend wrapping that export in quotes: export LD_LIBRARY_PATH="/bin/path../version/:$LD_LIBRARY_PATH", it's possible the variable setting is choking because it's not sure where the start/end is, would depend on the rest of the contents of your LD_LIBRARY_PATH.
    – Centimane
    Jul 26 '17 at 17:37
4

1

It can be exported locally.

#!/bin/bash

LD_LIBRARY_PATH=/bin/path../version/:$LD_LIBRARY_PATH path/to/executable/executable 

using VAR=value cmd will set and export VAR for cmd process.

2

export it in two step

#!/bin/bash

LD_LIBRARY_PATH=/bin/path../version/:$LD_LIBRARY_PATH 
export LD_LIBRARY_PATH

path/to/executable/executable 

this is the way I do in my scripts. (note that this contradict what man page says)

export [-fn] [name[=word]] ...

If a variable name is followed by =word, the value of the variable is set to word.

8
  • Thanks, I'm trying this now. I'm still a bit confused why placing the LD_LIBRARY_PATH before the executable without export changes things. Could you elaborate somewhat? Jul 26 '17 at 17:00
  • @ShanZhengYang Variables can be set in the same line as a command in bash, in which case the variable is only set/changed for that command. It's perhaps a bit more explicit, though it shouldn't function any differently.
    – Centimane
    Jul 26 '17 at 17:32
  • Just tried this approach. I'm getting the same error as before... Jul 26 '17 at 17:54
  • @Centimane Even export LD_LIBRARY_PATH="/bin/path../version/:$LD_LIBRARY_PATH" was a total failure Jul 26 '17 at 18:09
  • @ShanZhengYang What do you get if you echo $LD_LIBRARY_PATH after exporting it?
    – Centimane
    Jul 26 '17 at 18:11

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.