Reply
Thread Tools
Posts: 451 | Thanked: 334 times | Joined on Sep 2009
#1
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
from a shell script and have the exported environment variables stay in the system, after this shellscript has run. Can anyone help me?

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
all's fine and the PATH contains the proper dirs.

But if I create a shell script with just
Code:
. ~/.profile
and then run
Code:
./shellscript.sh
the PATH gets exported within the script, but once it finishes running the PATH in the term is obviously not affected by the export.

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.
 
Posts: 3,617 | Thanked: 2,412 times | Joined on Nov 2009 @ Cambridge, UK
#2
You need to call ". ./shellscript.sh", otherwise you're starting a separate shell to run it in, so all variables are lost afterwards.
 
Posts: 451 | Thanked: 334 times | Joined on Sep 2009
#3
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.
 
Posts: 151 | Thanked: 93 times | Joined on Sep 2009 @ sofia, bulgaria
#4
put

exec bash

at the end of your shell script. This will open a shell with already exported environment.
 
Posts: 726 | Thanked: 345 times | Joined on Apr 2010 @ Sweden
#5
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.
 
Posts: 451 | Thanked: 334 times | Joined on Sep 2009
#6
Originally Posted by lidow View Post
at the end of your shell script. This will open a shell with already exported environment.
I need the changes to stay active in the current shell, not a new instance...
 
Posts: 451 | Thanked: 334 times | Joined on Sep 2009
#7
Originally Posted by Joorin View Post
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.
I understand the logic, however I keep thinking there has to be a way to accomplish via a shell script the same effect in the current terminal that is achieved via sourcing a file, without having to source the top calling file.

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...
 
Posts: 726 | Thanked: 345 times | Joined on Apr 2010 @ Sweden
#8
Code:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

extern char **environ;

int main() {
  printf("environ[0]: %s\n", environ[0]);

  printf("PATH: %s\n", getenv("PATH"));

  if(!setenv("PATH", "/my/new/path", 1)) {
    printf("PATH: %s\n", getenv("PATH"));
  } else {
    printf("Could not change PATH variable!\n");
  }

  return 0;
}
 

The Following User Says Thank You to Joorin For This Useful Post:
Posts: 451 | Thanked: 334 times | Joined on Sep 2009
#9
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.
 
Posts: 151 | Thanked: 93 times | Joined on Sep 2009 @ sofia, bulgaria
#10
From exec manpage:
The exec() family of functions replaces the current process image with a new process image.
exec script.sh


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.
 

The Following User Says Thank You to lidow For This Useful Post:
Reply

Thread Tools

 
Forum Jump


All times are GMT. The time now is 17:32.