set library path for current script
Solution 1
In your script, these two lines close to the top should do the trick:
LD_LIBRARY_PATH="$(pwd)/lib"
export LD_LIBRARY_PATH
Although bash
allows you to set and export a variable in a single statement, not all shells do, so the two step approach is more portable, if that's a concern.
If this isn't working for you, check that you are running the script from the right place - using $(pwd)
like this ties you to running the script from the directory that contains the required ./lib
subdirectory.
If you want to be able to run the script from anywhere, you need to use the absolute path to the ./lib
subdir, or construct a relative path from the directory portion of the path to the script using, e.g., $(dirname $0)
Solution 2
you should execute you program in this way:
LD_LIBRARY_PATH=$(pwd)/lib/ <your_executable_here>
Related videos on Youtube
![nifker](https://i.stack.imgur.com/teiYx.png?s=256&g=1)
nifker
Updated on September 18, 2022Comments
-
nifker almost 2 years
How can I set the library path for the current script that's running? I mean I don't want to list a new path for the libraries in a textfile. I tried it using
export LD_LIBRARY_PATH=$(pwd)/lib/
This is the script:
#!/bin/bash LD_LIBRARY_PATH="$(pwd)/lib/" export LD_LIBRARY_PATH ./X3TC_config
-
Jazz about 9 yearsDo you really mean
$(pwd)
, or do you mean$(dirname $0)
? -
nifker about 9 yearsI mean $(pwd) .
-
Gilles 'SO- stop being evil' about 9 yearsThe script you've posted does exactly what you're asking. If that's not working for you, tell us where the libraries are located, what the current directory is when you start the script, and copy-paste the error messages complaining of a missing library. I suspect that you're asking the wrong question but I can't tell what the right question would be with so little information.
-
nifker about 9 yearsThe libs are in the lib folder but it still says it cant find the "libgtk-x11-2.0.so.0" library
-
-
Atul Vekariya about 9 years@D_Bye, probably. But if you need this setting only for this script your way will mess other programs
-
Anthon about 9 yearsThis doesn't change anything for the script that is running (which is what the OP asks for), only for a new invocation.
-
nifker about 9 yearsDo you mean with <your_execition_here> a library?
-
Atul Vekariya about 9 yearsNo, by <your_executable_here> i mean the program you run. If its for example script, named testscript.sh the command will become: LD_LIBRARY_PATH=$(pwd)/lib/ testscript.sh
-
nifker about 9 yearsI use the following script: "#!/bin/bash export LD_LIBRARY_PATH=$(pwd)/lib/ ./X3TC_config" To run a game outside from steam.
-
nifker about 9 yearsIsn't it possible to set library path in the script?
-
Jazz about 9 yearsIt is possible to export a variable in a script, so that it only affects calls made within that script. Once the shell started by the kernel to run the script dies, so does its context - including any variables you export. I should have made that clearer in my comment.
-
Atul Vekariya about 9 yearsYes, you can do it by executing your script on this way: . /path/to/script.sh This will run the script in current shell
-
nifker about 9 yearsI tried to run it like that but it says it can't find the libraries that are in the lib folder
-
Atul Vekariya about 9 yearsThis way is only if you have the set of variable inside the script
-
nifker about 9 yearsIt still says it can't the required "libgtk-x11-2.0.so.0" although it is in the folder.
-
Jazz about 9 yearsWhat is the command you are trying to call? Does it care about the value of
LD_LIBRARY_PATH
? -
nifker about 9 yearschmod +x testandlaunch ./testandlaunch
-
Jazz about 9 yearsIs
testandlaunch
the program you are trying to setLD_LIBRARY_PATH
for, or the script you are trying to modify to make that happen? It might help if you post the relevant parts of your script as an edit to your question. -
nifker about 9 yearsIt's the script.