I am using a system call to do some tasks
system('myframework mycode');
but it complains of missing environment variables. Those environment variables are set at my bash shell (from where I run the Perl code).
What am I doing wrong?
Does the system
call create a brand new shell (without environment variable settings)? How can I avoid that?
I think it's not the question of shell choice, since environment variables are always inherited by subprocesses unless cleaned up explicitly. Are you sure you have exported your variables? This will work:
This will work too:
This wouldn't:
I messed with environment variables being set for my script on this post where I needed the env variable $DBUS_SESSION_BUS_ADDRESS to be set, but it wouldn't when I called the script as root. You can read through that, but in the end you can check whether %ENV contains your needed variables and if not add them.
From perlvar
My problem was that I was running the script under sudo and that didn't preserve all my user's env variables, are you running the script under sudo or as some other user, say www-data (apache)?
Simple test:
and if that doesn't work then you will need to add it to %ENV at the top of your script.
try
system("echo \$SHELL");
on your system.system() calls /bin/sh as a shell. If you are on a somewhat different box like ARM it would be good to read the man page for the exec family of calls -- default behavior. You can invoke your .profile if you need to, since system() takes a command
It's complicated. Perl does not necessarily invoke a shell. Perldoc says:
So it actually looks like you would have the arguments passed right to execvp. Furthermore, whether the shell loaded your .bashrc, .profile, or .bash_profile depends on whether the shell is interactive. Likely it isn't, but you can check like this.
If you don't want to invoke a shell, call
system
with a list:If you want a specific shell, call it explicitly: