0

I have a simple environment setup script that exports some environment variables like so.

#!/bin/sh

export NEWROOT=~/some/directory
echo $NEWROOT

This echos the correct directory, but after its run, when I echo $NEWROOT in the same shell, it returns nothing.

Any idea why the variable isn't setting?

chris
  • 4,840
  • 5
  • 35
  • 66

2 Answers2

0

The shell is run in a separate process, and environment variables in a child process do not affect the environment variables in the parent process.

If you want to run the script in the same process, you can use the dot command, like this:

. myscript
Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
0

A child process can't affect the environment variables of its parent. If you source the script instead, that will evaluate the script in the current environment, leaving NEWROOT.

Kevin
  • 53,822
  • 15
  • 101
  • 132