Solution: Perl for “Can’t exec “export”: No such file or directory”

Solution: Perl for “Can’t exec “export”: No such file or directory”

While working with perl there are occasions where you need custom variables to be exported to bash/shell to execute certain shell commands.

The normal method which we try to export any variable in perl is using something like below:

system("export PATH=/some/custom/path/to/bin");

However, this does not work and it returns the following error message:

Can't exec "export": No such file or directory at myscript.pl line 11.

I came across the followig problem and found the solution for exporting the Environment Variables for shell/bash in the perl script.

Solution: Perl for “Can’t exec “export”: No such file or directory”

So the following is what you have to use in the perl script where you want to pass or export the Environment Variables:

$ENV{SOME_VARIABLE} = “VALUE”;

So for example, in the abov example the script should have the following line in the perl script.

$ENV{PATH} = “/some/custom/path/to/bin”;

Save and exit the file and you should get the appropriate results.

Solution: Perl for “Can’t exec “export”: No such file or directory”

The above solution should also apply for the cases where the user is trying to use setenv.

Another alternate solution which one can recommend is using a shell wrapper for the perl script. In such cases, a shell script executes the perl script. However, before it executes the script it sets the required Environment Variables or Variables from the shell script itself.

Considering the professional approach, I won’t recommend the use of such ‘hacks’ to export such variables.

The common variables that I can imagine where we need to set the Environment Variables or Variables are:

PATH
DISPLAY
VARIABLE
LD_LIBRARY_PATH

No Comments

Post a Comment

Time limit is exhausted. Please reload CAPTCHA.