LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 04-19-2004, 04:23 PM   #1
sylvain_gnu
Member
 
Registered: Mar 2004
Posts: 91

Rep: Reputation: Disabled
Angry Setting environment variables from a script...


Hello,

I wish to use a script from time to time to set and export some variables, but I don't understand, it doesn't work! I probably have missed something basic, but I really don't get it...

Example:

script '/usr/bin/japanese_env', containing the following:
LANG=ja_JP.eucJP
LC_MESSAGES=ja_JP.eucJP
LC_CTYPE=ja_JP.eucJP
XMODIFIERS=@im=kinput2
#
export LANG
export LC_MESSAGES
export LC_CTYPE
export XMODIFIERS

In a terminal (xterm for instance), I wish that when I launch 'japanese_env',
the script exports the 4 variables LANG, LC_MESSAGES, LC_CTYPE, XMODIFIERS...
Which seems logical to me, but it doesn't work...
%japanese_env
%echo $LC_MESSAGES
fr_FR
instead of 'ja_JP.eucJP' as I expected.

I don't understand why, and I cannot find the answer in shell manuals.

Can you help?

Cheers,
Sylvain.
 
Old 04-19-2004, 04:39 PM   #2
mandeltuete
Member
 
Registered: Mar 2003
Location: Switzerland
Distribution: Fedora 3
Posts: 75

Rep: Reputation: 15
You need to have
#!/bin/sh
at the beginning of your shell script. Have you?

Does
# export LC_MESSAGES=ja_JP.eucJP
work?

HTH
 
Old 04-19-2004, 06:58 PM   #3
sylvain_gnu
Member
 
Registered: Mar 2004
Posts: 91

Original Poster
Rep: Reputation: Disabled
Hello mandeltuete,

% export LC_MESSAGES=ja_JP.eucJP
% echo LC_MESSAGES
ja_JP.eucJP

This works just fine.
But if I write it in a script and then execute the script, then it doesn't work.

Another example:

% export BLA=blabla
% echo $BLA
blabla

Until now, it works, but let's see what happens when using a script:

% vi TRY_IT

and write this in TRY_IT:
#!/bin/sh (or another shell)
export BLA=hello

and then: execute TRY_IT,
% ./TRY_IT
% echo $BLA
blabla

=> So the script didn't really set the variable BLA ?
I'm confused.


Any ideas?
Sylvain.
 
Old 04-19-2004, 07:17 PM   #4
mandeltuete
Member
 
Registered: Mar 2003
Location: Switzerland
Distribution: Fedora 3
Posts: 75

Rep: Reputation: 15
Google was my friend

If you use export, it will export the variable to the child processes. But Bash is the parent process, so the variable isn't set in Bash. If you call another shell script out of your script with the export command, it will be accesible by the called script.

If you do:
# . my_script
or
# source my_script

It will export the variable to Bash.
 
Old 04-20-2004, 09:34 AM   #5
LittleFox
LQ Newbie
 
Registered: Mar 2004
Location: Belgium
Distribution: Debian testing & Ubuntu
Posts: 7

Rep: Reputation: 0
Mandeltuete is right.

When your script is launched, he 'receives' his own shell environment. This environment is destroyed when the script ends. So are all changes made to it, including 'exports'.

To overcome this, use of the '.' command (also known as 'source'). It will executes the script in the current shell.

Last edited by LittleFox; 04-20-2004 at 10:01 AM.
 
Old 04-20-2004, 01:31 PM   #6
sylvain_gnu
Member
 
Registered: Mar 2004
Posts: 91

Original Poster
Rep: Reputation: Disabled
Smile Thank you

Thank you guys!
This works fine
Sylvain.
 
  


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
Setting Environment Variables rbh123 Linux - Newbie 2 10-03-2005 07:34 PM
Setting environment variables from shell script theta Linux - General 5 09-02-2004 09:50 PM
Setting Environment Variables farzan Linux - Newbie 2 08-07-2004 10:13 AM
setting environment variables jayjoh Linux - Newbie 9 07-12-2004 01:35 PM
setting environment variables durden2.0 Linux - Newbie 5 07-15-2003 02:57 PM


All times are GMT -5. The time now is 07:53 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration