LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   importing variables into shell script? (https://www.linuxquestions.org/questions/programming-9/importing-variables-into-shell-script-662101/)

ocicat 08-11-2008 07:18 PM

importing variables into shell script?
 
Perhaps the title is incorrect, but here is the situation: in ~/.profile, I define value for variable foo. I am writing a script which needs the value of foo:
Code:

#!/bin/sh
echo "$foo"

Yet, upon execution, I simply see a blank line meaning that foo is not inherited into the shell executing the script. I know that foo is defined as I can see its value at the shell prompt:
Code:

$ echo $foo
value
$

For what it's worth, I'm running this within the Korn shell.

Any pointers would be appreciated.

jailbait 08-11-2008 07:27 PM

Different distributions use different files for shell initialization. Try .bashrc or .bashrc_profile. What distribution are you using?

---------------
Steve Stites

ocicat 08-11-2008 07:45 PM

Quote:

Originally Posted by jailbait (Post 3244169)
Try .bashrc or .bashrc_profile.

As stated initially, I'm using the Korn shell.

Mr. C. 08-11-2008 08:11 PM

Environment variables are exportable, and are typically in all CAPS:

Code:

$ FOO='some value'
$ echo $FOO
some value
$ sh -c 'echo $FOO'

$ export FOO
$ sh -c 'echo $FOO'
some value


burschik 08-12-2008 03:01 AM

If I recall correctly, .profile is only read by the login shell. Therefore, if foo is defined in .profile, but not exported, it will not be inherited by a subshell.

chrism01 08-12-2008 03:15 AM

Actually, ocicat, if you're used to the ksh, then specify that at the top of your script, not /bin/sh.
Also, as mentioned above, vars are not exported by default, you have to specify it as described by Mr C.


All times are GMT -5. The time now is 10:09 AM.