views:

74

answers:

2

I have a bash script as follows:

rvm use 1.8.7
rvm list

The first line is a function loaded within my .bashrc file that defines some enviroment variables. When executing the second line, those variables have been set to their previous values (the set values have been lost). What am I missing here?

Running on a ubuntu box.

+1  A: 

A subshell is being created and the variables are set within it. When the subshell exits, the changes are lost. This often happens when a while loop is in a pipe. Without seeing the function it's impossible to be more specific than that.

Dennis Williamson
Hello Dennis,This is the source for the function. Any suggestion?http://gist.github.com/449879
Guilherme Silveira
That's not a function, that's a script and there's also no functions defined within it. It does set some environment variables, though. If you `source` that file, it will set them in the current environment. If you run it instead, however, they won't persist into the current environment. Unless the variables are exported within this file when it's sourced near the end: `source $rvm_scripts_path/rvm`, they won't be available to this script/function when it's run right at the end: `rvm $*`. Please show the function which is defined somewhere like this: `rvm () { ... }` or `function rvm ...`.
Dennis Williamson
Hello Dennis,Sorry it was my mistake then. I thought it was a function because I read it somewhere else. The code avoe is within the rvm file that is sourced from bash_profile when the user logs in.After logging in, we run the CI server as "nohup java -war hudson.war" and this process will invoke a "build.sh" file:rvm use 1.8.7rvm listAnd thats when the second line does not have access to the values set from the first one.
Guilherme Silveira
@Guilherme: Each call in the script in your question is an independent subshell so variables don't persist from one to the next. It looks like you may be able to set some things in one of the rvmrc files or you might be able to change the first line above to `rvm use 1.8.7`. But it all depends on what's in the file that gets sourced here `source $rvm_scripts_path/rvm` in the file you posted to github. In what you've posted so far I see nothing getting exported so nothing is available to children of these scripts either (remember that, unless a file is sourced, variables it sets aren't...
Dennis Williamson
...available to the parent or its subsequent children).
Dennis Williamson
A: 

when you define environment variables that you want to make available to all subshells you need to prefix it with export like so:

export myvar="some value"

I would check that rvm is doing this properly

ennuikiller
But changes in subshells won't persist to the parent. Try this in Bash: `a=11; echo hi | while read b; do echo "startloop [$a] [$b]"; a=22; echo "inside [$a] [$b]"; done; echo "outside [$a] [$b]"`. The pipe into `while` creates a subshell. Now try it with `export`: `export a=11; echo hi|while read b; do echo "startloop [$a] [$b]"; a=22; echo "inside [$a] [$b]"; done; echo "outside [$a] [$b]"`
Dennis Williamson
You can avoid the subshell like this: `while ... done < <(echo hi)` or `while ... done <<< hi`
Dennis Williamson
yes this is true, you never want shell variables to potentially pollute the parent shell. however, if you source the rvm script then all environment variables defined in rvm will be available in the parent shelll
ennuikiller
when defining the rvm function is it actually "source .rvm/bin/rvm".but invoking rvm is just rvm ... should i use source rvm?
Guilherme Silveira