lef
lef

Reputation: 1234

OSX exported environment variables not inherited in child process

I suffer from really strange problem on Snow Leopard. In my terminal, I wrote two scripts:

First:

#script-1.sh
export MY_VAR="This is my variable"

Second script:

# script-2.sh, having +x permission
#!/bin/bash
echo $MY_VAR

In Terminal, if I source the script-1 and then call script-2 as a child process, no output is displayed:

$> source script-1.sh
$> echo $MY_VAR
This is my variable
$> . script-2.sh
This is my variable
$> ./script-2.sh

$>

Any idea what is wrong here? I tried the same scenario on Windows using Cygwin, and there it works as expected - on OSX, it seems, that the child process does not know $MY_VAR at all.

Upvotes: 2

Views: 1046

Answers (1)

Kalicz
Kalicz

Reputation: 346

Are you sure you didn't run script-1.sh in child process? I tried your example in 10.6.8 and 10.7.3 and it works properly. Command source script-1.sh is same as . script-1.sh and it runs script in same context. Exported variables are environmental variables and they are always copied to child process.

Only scenario when I was able to reproduce same output as you have is, when I omitted export from script-1.sh file.

Upvotes: 1

Related Questions