# Unix/Linux environment variables for simpletons

**URL:** https://boards.straightdope.com/t/unix-linux-environment-variables-for-simpletons/483925
**Category:** Factual Questions
**Created:** [January 31, 2009, 10:54pm UTC](https://boards.straightdope.com/t/unix-linux-environment-variables-for-simpletons/483925 "2009-01-31T22:54:02Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ticker](https://avatars.discourse-cdn.com/v4/letter/t/d07c76/32.png) [@ticker](https://boards.straightdope.com/u/ticker)
#### Post date: [January 31, 2009, 10:54pm UTC](https://boards.straightdope.com/t/unix-linux-environment-variables-for-simpletons/483925/1 "2009-01-31T22:54:02Z")

</div>

I am new to the Unix/Linux world and could do with some help regarding environment variables, shell scripts and the like. As I understand it if I have the following in a shell script:

```auto

MYVAR=somevalue
export MYVAR

```

then $MYVAR is available within the remainder of the script and to any commands called within it, but not to the shell that called the script. This thwarted my naive plan to create a script which set up a bunch of environment variables, which could then be called from other scripts as required :(. Is there a simple way to achieve what I was trying to do.

---

<div class="post-metadata">

### Author: ![Rysto](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@Rysto](https://boards.straightdope.com/u/Rysto)
#### Post date: [January 31, 2009, 11:03pm UTC](https://boards.straightdope.com/t/unix-linux-environment-variables-for-simpletons/483925/2 "2009-01-31T23:03:40Z")

</div>

What you want to do is _source_ the script that sets the variables:

/etc/variables.conf:

```auto

MY_VAR=value

```

my\_script.sh:

```auto

#!/bin/sh

. /etc/variables.conf

echo $MY_VAR

```

Run my\_script.sh, and it will print “value”.

---

<div class="post-metadata">

### Author: ![ticker](https://avatars.discourse-cdn.com/v4/letter/t/d07c76/32.png) [@ticker](https://boards.straightdope.com/u/ticker)
#### Post date: [January 31, 2009, 11:17pm UTC](https://boards.straightdope.com/t/unix-linux-environment-variables-for-simpletons/483925/3 "2009-01-31T23:17:31Z")

</div>

Simple :smack:, Cool 🆒, Thanks 🙂

---

<div class="post-metadata">

### Author: ![Digital\_Stimulus](https://avatars.discourse-cdn.com/v4/letter/d/aeb1de/32.png) [@Digital\_Stimulus](https://boards.straightdope.com/u/Digital_Stimulus)
#### Post date: [January 31, 2009, 11:24pm UTC](https://boards.straightdope.com/t/unix-linux-environment-variables-for-simpletons/483925/4 "2009-01-31T23:24:56Z")

</div>

Also, be aware that various shells have different commands. For instance, _export_ isn’t universal; in csh, you would use _setenv_.

In addition, I’m pretty sure that not all shells recognize “.” as equivalent to “source”.

---

<div class="post-metadata">

### Author: ![Pasta](https://avatars.discourse-cdn.com/v4/letter/p/ecccb3/32.png) [@Pasta](https://boards.straightdope.com/u/Pasta)
#### Post date: [February 1, 2009, 1:42am UTC](https://boards.straightdope.com/t/unix-linux-environment-variables-for-simpletons/483925/5 "2009-02-01T01:42:18Z")

</div>

To elaborate…

If you have a script that you’d like to run in a new subshell (so that when it ends, your current environment is unchanged) you do what you did in the OP. If you want to run the lines of the script in your current shell, thereby modifying its environment, you have to “source” the script, like so:

contents of blah.sh:

```auto

export MYVAR="foobar"
export MYOTHERVAR=65

```

Then at your prompt:

```auto

myprompt> source blah.sh

```

Bash has an alias for “source”, namely the period (.), so you could type “. blah.sh” instead of “source blah.sh”. Tcsh doesn’t have this. To find out what shell you are running, type “ps” and look for “bash” or “tcsh”. (It is possible but unlikely that you are running a shell other than these.)

The tcsh equivalent of the above script would be:

```auto

setenv MYVAR "foobar"
setenv MYOTHERVAR 65

```

---

<div class="post-metadata">

### Author: ![Rysto](https://avatars.discourse-cdn.com/v4/letter/r/ecccb3/32.png) [@Rysto](https://boards.straightdope.com/u/Rysto)
#### Post date: [February 1, 2009, 2:17am UTC](https://boards.straightdope.com/t/unix-linux-environment-variables-for-simpletons/483925/6 "2009-02-01T02:17:30Z")

</div>

> [@Pasta](#):
>
> Bash has an alias for “source”, namely the period (.), so you could type “. blah.sh” instead of “source blah.sh”.

I believe that . works in all bourne shells, but don’t quote me on that,

> [@](#):
>
> To find out what shell you are running, type “ps” and look for “bash” or “tcsh”. (It is possible but unlikely that you are running a shell other than these.)

The $SHELL environment variable should be set to the path of the shell.
