0

I work with a opensource software that I have built locally. After build the manual says to run it like this while inside the build directory

$ LD_LIBRARY_PATH=../applicationExeFile

Then the application runs fine.

Now I tried to make a simple shell to call quickly without typing much (run.sh) with the following content:

#!/bin/bash

export -n LD_LIBRARY_PATH=.
./applicationExeFile

But it seems that LD_LIBRARY_PATH does not get registered in shell as I get error regarding the application executable can not find a library that is in the same folder

./applicationExeFile: error while loading shared libraries: libchart.so: cannot open shared object file: No such file or directory

What am I doing wrong and how to achieve this?

4
  • What are you supposed to set it to? You seem to be setting the variable to ., is that what you want?
    – terdon
    Commented Jan 24, 2023 at 11:20
  • yes I need to set it to '.' as in the current directory
    – DEKKER
    Commented Jan 24, 2023 at 11:30
  • Why did you add -n to the export command? that's for removing a variable from the environment Commented Jan 24, 2023 at 11:35
  • What steeldriver said. Also, what current directory? The way you have it set up, it will only work if you first cd into the target directory and only then run the script.
    – terdon
    Commented Jan 24, 2023 at 11:37

1 Answer 1

3

The first issue is that -n actually unexports a variable, it doesn't export it. See help export:

export: export [-fn] [name[=value] ...] or export -p
    Set export attribute for shell variables.
    
    Marks each NAME for automatic export to the environment of subsequently
    executed commands.  If VALUE is supplied, assign VALUE before exporting.
    
    Options:
      -f    refer to shell functions
      -n    remove the export property from each NAME
      -p    display a list of all exported variables and functions
    
    An argument of `--' disables further option processing.
    
    Exit Status:
    Returns success unless an invalid option is given or NAME is invalid.

Next, . refers to the current working directory of the process. This means that your script still needs to be run from the specific directory where your program is installed which seems a bit pointless. Just use a full path instead. If your program is at /home/dekker/myprogram, make your script like this:

#!/bin/bash
export LD_LIBRARY_PATH=/home/dekker/myprogram
/home/dekker/myprogram/applicationExeFile

Or just:

#! /bin/sh -
LD_LIBRARY_PATH=/home/dekker/myprogram exec /home/dekker/myprogram/applicationExeFile

as there's nothing bash-specific, and you don't need the separate export statement. Using exec also saves a process and makes signal handling and the reporting of the exit status more reliable.

2
  • Thanks! Also about the exec I didn't know that :)
    – DEKKER
    Commented Jan 24, 2023 at 14:15
  • You're very welcome, @DEKKER, although the exec bit came from Stéphane who edited my answer, making it much better. So all credit for that bit goes to him! :)
    – terdon
    Commented Jan 24, 2023 at 14:46

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.