1

I am running a perl file a.pm which invokes b.sh via system command. Here, b.sh is using find utility whose path is /usr/local/bin.

If I run env on shell directly on machine, I get output as below for PATH variable.

PATH=/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/home/bin:/home/bin/samba::/home/venv/bin/:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin`

Thats why if I run the b.sh directly from shell, it is able to execute find utility.

Now, If I run b.sh via a.pm as mentioned earlier using system(), and when I print PATH env variable in b.sh, its coming as

/bin:/usr/bin:/usr/X11R6/bin:/home/bin:/home/perl5/bin

which does not have /usr/local/bin, and thats why find command is failing.

If I tried to print all ENV variables in perl before invoking system(b.sh), PATH variable is not printed.

Now, I tried adding path variable in a.pm file as follows just before invoking system(b.sh).

$ENV{'PATH'} = '/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin:/home/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/';

Now, if I try to print all ENV variables in perl before invoking system(b.sh), PATH variable is printed with above value.

Still executing the a.pm file, the PATH variable printed in b.sh is same:

/bin:/usr/bin:/usr/X11R6/bin:/home/bin:/home/perl5/bin

How can I add corresponding path /usr/local/bin to shell of b.sh invoked using a.pm?

8
  • How do you invoke your shell script? Does b.sh have a shebang? What is it? Commented Mar 19, 2021 at 18:13
  • I am using system(b.sh) in a.pm file. I can run b.sh directly from linux shell as well and then it works fine. Commented Mar 19, 2021 at 18:14
  • 1
    No program can ever change its parent process's environment variables. This is part of how environment variables work on UNIX in general -- they're copied from parent to child, never from child to parent. Commented Mar 19, 2021 at 18:17
  • ...mind, changing $ENV{'PATH'} does change the PATH for future subprocesses started from that same Perl interpreter. A minimal reproducible example that we can run without changes to see if that isn't working would be an appropriate place to start. Commented Mar 19, 2021 at 18:18
  • 1
    Note that if you have an ENV or BASH_ENV that points to the name of a file that overrides PATH, that's one way for the shell started up by system() to have the PATH set by the parent process overridden. Commented Mar 19, 2021 at 18:19

1 Answer 1

3

I suspect that the Perl program is either modifying the path it gets from the shell that invokes it, or that you have left out a step somewhere. For example, if you invoke the Perl program from a different environment, it will likely have a different PATH.

You seemed to have found your answer though. Add the necessary directory to the PATH in the Perl program. But, you say this doesn't work. Again, I think there's some step that you haven't included. I suspect that the way in which you run system overwrites the PATH inherited from the parent.

For example, here's a small Perl program the merely runs a shell script:

#!perl
use v5.10;

$ENV{PATH} = '/bin:/usr/bin';

say "PATH in Perl is $ENV{PATH}";
system( "sh ./pather.sh" );

The shell script echos the PATH:

#!/bin/sh

echo "PATH in shell:" $PATH

When I run this, both PATHs match:

PATH in Perl is /bin:/usr/bin
PATH in shell: /bin:/usr/bin

But, maybe the command in system is something else. The -l switch treats the shell as a login shell, so it will load the various profiles and whatnot:

system( "sh -l ./pather.sh" );

Now the PATH is different in the shell script because my particular profiles overwrote PATH:

PATH in Perl is /bin:/usr/bin
Path in shell: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/usr/local/go/bin:...

Our answers can be more targeted you can produce a minimal working example where we see actual code that demonstrates the problem. Since we don't see what you actually run in system, we can only guess.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.