LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Setting environment variables from a script... (http://www.linuxquestions.org/questions/linux-newbie-8/setting-environment-variables-from-a-script-172170/)

sylvain_gnu 04-19-2004 04:23 PM

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.

mandeltuete 04-19-2004 04:39 PM

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

Does
# export LC_MESSAGES=ja_JP.eucJP
work?

HTH

sylvain_gnu 04-19-2004 06:58 PM

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.
:confused:

Any ideas?
Sylvain.

mandeltuete 04-19-2004 07:17 PM

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.

LittleFox 04-20-2004 09:34 AM

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.

sylvain_gnu 04-20-2004 01:31 PM

Thank you
 
Thank you guys!
This works fine :)
Sylvain.


All times are GMT -5. The time now is 11:32 PM.