# Setting environment variable using shell script

**URL:** <https://community.unix.com/t/setting-environment-variable-using-shell-script/269012>\
**Category:** Shell Programming and Scripting\
**Created:** [July 7, 2010, 5:19am UTC](https://community.unix.com/t/setting-environment-variable-using-shell-script/269012 "2010-07-07T05:19:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![arun\_maffy](https://community.unix.com/letter_avatar/arun_maffy/32/6_22734026db12803ebb3e059622ac3534.png) [@arun\_maffy](https://community.unix.com/u/arun_maffy)\
**Post date:** [July 7, 2010, 5:19am UTC](https://community.unix.com/t/setting-environment-variable-using-shell-script/269012/1 "2010-07-07T05:19:56Z")

</div>

Hi All,

I'm trying to write an menu driven program to automate some functions which involve loging to multiple hosts. The hosts can differ for every use, so I thought I would use an config file to get the hostnames. Now I need to set those values in the config file to environment variable to access by the menu script to login and proceed with the functions.

I tried to execute the config file from the shell script, but the environment variables are dying after the menu script is exited. The question is how to set those environment variable in the terminal for the complete session even after exiting the script, because if the menu gets failed in the 2nd or 3rd step and when the user tries to execute the script again it is mandating the user to run the config file inorder to set the env variables which is a pain for the user.

Please give your suggestions !!!

Thanks,

---

<div class="post-metadata">

**Author:** ![agn](https://community.unix.com/letter_avatar/agn/32/6_22734026db12803ebb3e059622ac3534.png) [@agn](https://community.unix.com/u/agn)\
**Post date:** [July 7, 2010, 5:27am UTC](https://community.unix.com/t/setting-environment-variable-using-shell-script/269012/2 "2010-07-07T05:27:13Z")

</div>

You could try source'ing your config

```nohighlight
$ source config.file

```

Or

```nohighlight
$ . config.file

```

---

<div class="post-metadata">

**Author:** ![arun\_maffy](https://community.unix.com/letter_avatar/arun_maffy/32/6_22734026db12803ebb3e059622ac3534.png) [@arun\_maffy](https://community.unix.com/u/arun_maffy)\
**Post date:** [July 7, 2010, 6:01am UTC](https://community.unix.com/t/setting-environment-variable-using-shell-script/269012/3 "2010-07-07T06:01:43Z")

</div>

If my understanding is right, the source command is used to use the external file for the menu script which it is being called. But my query is once I execute the config file the env variables should be set not only to the child process which is created but to the parent process where the menu script is executed.

please let me know if you couldn't catch my point.

Thanks for your help

---

<div class="post-metadata">

**Author:** ![guruprasadpr](https://community.unix.com/user_avatar/community.unix.com/guruprasadpr/32/1542_2.png) [@guruprasadpr](https://community.unix.com/u/guruprasadpr)\
**Post date:** [July 7, 2010, 6:07am UTC](https://community.unix.com/t/setting-environment-variable-using-shell-script/269012/4 "2010-07-07T06:07:22Z")

</div>

Arun  
Newly set env variables are not available for the parent shell. However, if you need, what you can do is:

\> Use 'printenv' command and redirect the output to a file from your function.  
\> Run this file once you are back in the main menu either using . or source depending on your shell.

Guru.

---

<div class="post-metadata">

**Author:** ![arun\_maffy](https://community.unix.com/letter_avatar/arun_maffy/32/6_22734026db12803ebb3e059622ac3534.png) [@arun\_maffy](https://community.unix.com/u/arun_maffy)\
**Post date:** [July 7, 2010, 6:22am UTC](https://community.unix.com/t/setting-environment-variable-using-shell-script/269012/5 "2010-07-07T06:22:09Z")

</div>

Even though I run while returning back to the main menu it doesn't help me because once the script exits the env variables would die, so there is no way I can set the env variable to the parent process ??

---

<div class="post-metadata">

**Author:** ![guruprasadpr](https://community.unix.com/user_avatar/community.unix.com/guruprasadpr/32/1542_2.png) [@guruprasadpr](https://community.unix.com/u/guruprasadpr)\
**Post date:** [July 7, 2010, 6:47am UTC](https://community.unix.com/t/setting-environment-variable-using-shell-script/269012/6 "2010-07-07T06:47:00Z")

</div>

Arun  
This is what i meant:

File f2:

```nohighlight
#!/usr/bin/ksh

echo in file f2
export X="welcome"
printenv |grep -v printenv | sed 's/^/export /' > f3

```

File f1:

```nohighlight
#!/usr/bin/ksh

echo in main f1
f2
. f3
echo X is $X

```

On running the file f1:

```nohighlight
in main f1
in file f2
X is welcome

```

X is set in the child shell f2, and we got it back in the parent f1.

Guru.

---

<div class="post-metadata">

**Author:** ![arun\_maffy](https://community.unix.com/letter_avatar/arun_maffy/32/6_22734026db12803ebb3e059622ac3534.png) [@arun\_maffy](https://community.unix.com/u/arun_maffy)\
**Post date:** [July 7, 2010, 8:49am UTC](https://community.unix.com/t/setting-environment-variable-using-shell-script/269012/7 "2010-07-07T08:49:42Z")

</div>

Guru,

Thanks for the explanation.
