Setting environment variables from a file : | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Setting environment variables from a file :

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-05-2012
qwertyu qwertyu is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 21 January 2013, 5:12 AM EST
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
Setting environment variables from a file :

Hi,

I have around 10 environment variables in my shell script. i want to set this all in a file and just call that file in my shell script. How can i do that ? Please help. TIA!
Sponsored Links
    #2  
Old 12-05-2012
Skrynesaver's Avatar
Skrynesaver Skrynesaver is online now Forum Advisor  
Grumpy old geek
 
Join Date: Mar 2011
Last Activity: 18 December 2014, 8:12 AM EST
Location: Éire
Posts: 830
Thanks: 20
Thanked 187 Times in 183 Posts
Simply enter them in the form

Code:
KEY=value

one to a line in the file (called config.sh in this example), then execute the commands in the file in the script

Code:
#!/bin/bash
. config.sh
env

Sponsored Links
    #3  
Old 12-05-2012
qwertyu qwertyu is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 21 January 2013, 5:12 AM EST
Posts: 15
Thanks: 1
Thanked 0 Times in 0 Posts
setting environment variables from file

Hi,

I tried that approach, it doesnt seem to work.. is there any other way. I am using Korn shell btw.
    #4  
Old 12-05-2012
phunk phunk is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 28 February 2014, 3:57 PM EST
Posts: 35
Thanks: 3
Thanked 1 Time in 1 Post
Yea, similar to above for me.

File called env.vars:

Code:
VARIABLE=Value;  EXPORT VARIABLE
VARIABLE2=Value2; EXPORT VARIABLE2


Code:
#!/bin/ksh

#Set environment variables.
. env.vars

Sponsored Links
    #5  
Old 12-05-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 17 December 2014, 4:46 PM EST
Location: Saskatchewan
Posts: 20,015
Thanks: 855
Thanked 3,448 Times in 3,221 Posts
Quote:
Originally Posted by qwertyu View Post
Hi,

I tried that approach, it doesnt seem to work..
In what way does it 'not work'? Show exactly what you did, word for word, letter for letter, keystroke for keystroke, because that is the proper method for reading variables from a script.

Note how he does not just run the script. He sources it. . script Dot space, not dot-slash.
Sponsored Links
    #6  
Old 12-05-2012
gary_w's Avatar
gary_w gary_w is offline
Registered User
 
Join Date: Oct 2010
Last Activity: 10 December 2014, 5:14 PM EST
Posts: 446
Thanks: 32
Thanked 96 Times in 88 Posts
Quote:
Originally Posted by qwertyu View Post
Hi,

I tried that approach, it doesnt seem to work.. is there any other way. I am using Korn shell btw.
Your chances for help would improve if you showed examples of how you are currently trying it and what the output is.
Sponsored Links
    #7  
Old 12-05-2012
Skrynesaver's Avatar
Skrynesaver Skrynesaver is online now Forum Advisor  
Grumpy old geek
 
Join Date: Mar 2011
Last Activity: 18 December 2014, 8:12 AM EST
Location: Éire
Posts: 830
Thanks: 20
Thanked 187 Times in 183 Posts
Ah, you are using the Korn shell, therefore you need to provide the full path to the config file (or have it in your PATH). Korn assumes it should search the PATH unless a path is supplied to the shell (odd but true), ie

Code:
#!/bin/ksh
. ./config.sh


Last edited by Skrynesaver; 12-05-2012 at 12:09 PM.. Reason: added example for clarity
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Setting environment variables in Cron file shyamu544 Shell Programming and Scripting 4 07-20-2011 05:43 AM
Setting up environment variables exchequer598 UNIX for Dummies Questions & Answers 2 09-03-2010 01:10 AM
Setting Environment Variables bigubosu UNIX for Dummies Questions & Answers 4 04-30-2010 07:23 AM
Need help with setting up environment variables SSSB UNIX for Dummies Questions & Answers 1 01-21-2010 03:44 PM
Setting up Environment Variables rpandey Shell Programming and Scripting 6 06-06-2005 10:28 AM



All times are GMT -4. The time now is 09:26 AM.