Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set LD_PRELOAD when executing a command in shell script

Tags:

linux

bash

shell

I want to execute a command like this: "LD_PRELOAD=/path/to/my/so ./a.out"

so I wrote a shell script:

cmd="LD_PRELOAD=/path/to/my/so ./a.out"
${cmd}

Error occured:

LD_PRELOAD=/path/to/my/so : no such file or directory

By the way, the file /path/to/my/so exists and I can successfully execute the command in a bash.

Anything wrong?

like image 482
Coaku Avatar asked May 24 '26 03:05

Coaku


2 Answers

It would be more traditional to just do something like this in your script:

export LD_PRELOAD=whatever
./a.out
like image 93
Ernest Friedman-Hill Avatar answered May 26 '26 16:05

Ernest Friedman-Hill


It's looking for an executable called LD_PRELOAD=/path/to/my/so in your path and can't find it. You can use eval to get around this:

eval $CMD

Or, equivalently:

bash -c "$CMD"
like image 33
Cairnarvon Avatar answered May 26 '26 17:05

Cairnarvon