Problem setting environment variables from script

Hi all!

I know that environment variables can be set on the .bashrc file, but I need to set them from a sh script. I saw a lot of websites that teach this but it doesn't work for me.

#!/bin/sh
DEKTOP=$DESKTOP=:/home/rrodrigues/Desktop
export DESKTOP

if I do echo $DESKTOP returns me nothing.

Why that?

You have a typo in your code, this should work:

#!/bin/sh
DESKTOP=$DESKTOP:/home/rrodrigues/Desktop
export DESKTOP

Be aware, that this script does not change your login shell's environment. It only changes the environment of the script and commands invoked by this script.

If your script is not .bashrc and you want to make the variable available in your shell environment (ie, the command line), then you must "source" the script like this:

 . myscript

This also will work

#!/bin/sh
export DESKTOP=$DESKTOP:/home/rrodrigues/Desktop

Not in my sh!

http://www.informatik.uni-frankfurt.de/doc/man/hpux/sh-bourne.1.html
 
      export [name ...]   The given names are marked for automatic export to
                          the environment of subsequently-executed commands.
                          If no arguments are given, a list of names
                          currently included in the environment are printed.
                          Function names cannot be exported.

The classic error is to run the environment script normally, as a child, and then only that child and its children get to see it, but then that child and its children end and the parent and next child are unaffected.

The trick is that after you set a variable, only you see it, but when you export the variable, the receiving process will be your child. The ". myscript" sourcing above runs the script in the current shell so it affects you and your children.

Scripts pathed $HOME/.(shell_name)rc like ~/.kshrc and ~/.bashrc are run automatically as the ksh or bash process comes to life, which is nice but excessively often, as script calls script after script. The bourne shell sh only uses ~/.profile, but for that reason and the superset offered in ksh, few people use it interactively. Other shell programs run ~/.profile once for the login shell only.

Create a config file with the variables that you want to set.
The file will have variables like (this is just an example)
export TWO_TASK=GJEUGHD
export LD_LIBRARY_PATH_64=/opt/oracle/product/10.2.0/lib
export ORACLE_BASE=/oracle
export ORA_DEFAULT=true

Then source it

source /opt/cdrapp/config/env.cfg

Hope this helps.

The "source" command is a "bash" command. Most other shells don't support it.
The dot-space-scriptname method works with most Bourne-type shells.

1 Like

.myscript

Surely
. ./myscript

Or if the script can be found in the current $PATH:
. myscript

Methyl, thanks for the post and the lesson. I assume the dot-space-scriptname (ie . /opt/cdrapp/config/env.cfg) will work even with bash, so this is a much better way.

Thanks again methyl

It is best practices to use a definite path, like ~/.profile or ./.bash_rc !