LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   No effect when 'export' command executed in a script? (https://www.linuxquestions.org/questions/linux-software-2/no-effect-when-export-command-executed-in-a-script-172174/)

sylvain_gnu 04-19-2004 03:37 PM

No effect when 'export' command executed in 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.

Komakino 04-19-2004 03:53 PM

Having % as part of the prompt makes it look to me like you're using something other than bash (csh perhaps?). export works only for bourne shell derivatives. Which shell are you using in the script?

sylvain_gnu 04-19-2004 05:49 PM

zsh is the shell I'm using, same problem with bash...
 
Hi Komakino,

I'm using zsh, and I observed the same problem with bash.

Example (with bash or with zsh):

% ~/TRUC contains the line export BID=blabla

Then I do chmod +x TRUC and then ./TRUC , and then echo $BID shows nothing!
So 'export doesn't work in a script' ?

On the other hand, export *in a command line* works fine:
% export BID=something
% echo $BID
something

Why's that? Any ideas?
Cheers,
Sylvain.

Scruff 04-19-2004 06:27 PM

Try initializing and exporting them in the same line like:

#!/bin/sh
export LC_MESSAGES=ja_JP.eucJP
export LC_CTYPE=ja_JP.eucJP

I did this, then chmod +x'd it, ./script, then %echo $LC_MESSAGES and it worked fine.

sylvain_gnu 04-20-2004 12:39 PM

Solution
 
This doesn't work either.

On the other hand, someone gave me the solution I was looking for.

% . ./script
and the variables exported by 'script' will be available in the current shell.
% source ./script
has the same effect.

Cheers,
Sylvain.

Scruff 04-20-2004 05:33 PM

I must have made a mistake last night when I tried it cause it doesn't work for me now. I probably exported it right in the shell and then when I tried it in a script it looked like it worked. Exported variables are only supposed to effect child processes (unless done as you mentioned above), so since the terminal is parent to the script, the variable doesn't get passed to it.

I am still kinda new at shell programming myself ;)

sylvain_gnu 04-20-2004 07:15 PM

Ok!
 
No problem. Thanks anyway for yesterday.
And we have the solution now!
See you.
Sylvain.


All times are GMT -5. The time now is 05:55 AM.