![]() |
How to source ~/.profile through a script with effects lasting after the script has run?
I have customized PATH, CFLAGS, etc many variables in .profile, which get changed dynamically upon copying files to certain directories.
After I do this, to make the system aware, I source ~/.profile again, which makes it reread these dirs. I've been busting my brains, how to call: Code:
. ~/.profile What I mean is, for instance: say .profile reads ~/bin for subdirs and then adds these to the PATH So I source it now, it only has one subdir, so only ~/bin/1 gets added to the PATH Let's say I create ~/bin/2, and now I need to source ~/.profile to add ~/bin/1 and ~/bin/2 to PATH If I open a terminal and run Code:
. ~/.profile But if I create a shell script with just Code:
. ~/.profile Code:
./shellscript.sh I want to source .profile in a shellscript somehow and have the resultant exported variables remain exported after the shellscript has finished running. Can I do this somehow? Can't figure out how... Thanks for advice. |
Re: How to source ~/.profile through a script with effects lasting after the script has run?
You need to call ". ./shellscript.sh", otherwise you're starting a separate shell to run it in, so all variables are lost afterwards.
|
Re: How to source ~/.profile through a script with effects lasting after the script has run?
Yeah, but that's exactly the thing, that's not solving anything, there's no difference between sourcing .profile and shellscript.sh
I cannot source anything, I can just call a script. That's because I need to incorporate the sourcing into other scripts which manipulate these files based on criteria etc. and then after all is done the variables should be sourced in the system. That's why I can't source the master shellscript, because I call it to do some dynamic operations, give it parameters of directories to create etc. and then when it's done, I need .profile or any file to automatically be sourced and variables exported. |
Re: How to source ~/.profile through a script with effects lasting after the script has run?
put
exec bash at the end of your shell script. This will open a shell with already exported environment. |
Re: How to source ~/.profile through a script with effects lasting after the script has run?
Environment variables are, by definition, bound to a certain process.
If you spawn a child process, it inherits the environment. So, if you want your changes to stay available you need to run the process in the same environment as the one you changed. |
Re: How to source ~/.profile through a script with effects lasting after the script has run?
Quote:
|
Re: How to source ~/.profile through a script with effects lasting after the script has run?
Quote:
Why I don't want to source the file is simple: I want the whole procedure to be callable via a command, that I can put in the path, so that the user doesn't have to worry about sourcing something. But I want to be able to change the environment variables for the given shell environment and all the future spawned processes dynamically. I keep thinking this has to be doable somehow... |
Re: How to source ~/.profile through a script with effects lasting after the script has run?
Code:
#include <unistd.h> |
Re: How to source ~/.profile through a script with effects lasting after the script has run?
Same thing though. Compile and run in a terminal, while it's running it changes it. Once command finishes, the old PATH is in effect.
I'm trying to change the PATH and other variables in a running shell via a command callable in a script, that would have a permanent effect. |
Re: How to source ~/.profile through a script with effects lasting after the script has run?
From exec manpage:
Quote:
where in script.sh you source environment and then exec bash, will substitute your current shell with the one from the script. This will give you all environments you need, and will not open chain of subshells. |
All times are GMT. The time now is 16:27. |
vBulletin® Version 3.8.8