LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-11-2008, 07:18 PM   #1
ocicat
Member
 
Registered: May 2007
Posts: 208

Rep: Reputation: 48
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.
 
Old 08-11-2008, 07:27 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Virginia, USA
Distribution: Debian 12
Posts: 8,334

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

---------------
Steve Stites
 
Old 08-11-2008, 07:45 PM   #3
ocicat
Member
 
Registered: May 2007
Posts: 208

Original Poster
Rep: Reputation: 48
Quote:
Originally Posted by jailbait View Post
Try .bashrc or .bashrc_profile.
As stated initially, I'm using the Korn shell.
 
Old 08-11-2008, 08:11 PM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
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
 
Old 08-12-2008, 03:01 AM   #5
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
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.
 
Old 08-12-2008, 03:15 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
pass php variables to shell script viveksnv Programming 1 03-04-2008 11:46 PM
Shell script variables Fredde87 Linux - Newbie 13 11-01-2006 03:35 AM
shell script variables Gary_Menegon Programming 1 10-02-2006 09:28 AM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 04:32 AM
Why?? can not use variables with shell script Bassam Linux - General 9 01-27-2004 07:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration