Setting environment variables from a script...
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...
script '/usr/bin/japanese_env', containing the following:
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...
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?
You need to have
at the beginning of your shell script. Have you? :)
# export LC_MESSAGES=ja_JP.eucJP
% export LC_MESSAGES=ja_JP.eucJP
% echo LC_MESSAGES
This works just fine.
But if I write it in a script and then execute the script, then it doesn't work.
% export BLA=blabla
% echo $BLA
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)
and then: execute TRY_IT,
% echo $BLA
=> So the script didn't really set the variable BLA ?
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
# source my_script
It will export the variable to Bash.
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.
Thank you guys!
This works fine :)
|All times are GMT -5. The time now is 09:14 AM.|