1

I'm trying to set a shell variable to $PWD - the path from were the script is executed. This is what I have, setenv.sh:

#!/bin/bash
export WORK_AREA=$PWD      #also tried "$PWD", $(pwd), `pwd`

But when I run from the shell

> ./setenv.sh
> echo $WORK_AREA

WORK_AREA has no value

Meir
  • 151

2 Answers2

0

export works "downwards" not "upwards". When you export a value in the script that's a child of the shell, it will not export it to the shell that's its parent. It will only change the environment for its own children.

0

You need to source the script if you want it to affect your current shell environment, e.g.:

source setenv.sh

Or, using the dot-notation:

. setenv.sh

When you execute a script it runs in a subshell, as if you had run it using the bash command. Changes in the subshell environment do not affect the parent environment. Note that we're only talking about environment variables here. If you modify a file in a subshell then that change will definitely carry over to the parent shell.

See the related SuperUser post:

igal
  • 9,886