3

I want to set an environment variable from a bash script that I wrote. So I created a bash script and called it set.sh. Its content is as follows:

#!/bin/bash

export DEV_SRC="/home/m/mydata/sourecCode"
echo $DEV_SRC

When I run this script, the output is

/home/m/mydata/sourecCode

But if I run this code on the same terminal that I ran the above script from,

echo $DEV_SRC

I cannot see any value, so I think the value is not exported.

Why is the value not exported?

Zanna
  • 70,465
mans
  • 189

1 Answers1

5

By default bash creates a copy of the current environment, executes the script in this environment, then destroys the copy.

To execute a script in the current environment you should use this syntax:

. /home/m/mydata/sourecCode
echo $DEV_SRC

or

source /home/m/mydata/sourecCode
echo $DEV_SRC
Zanna
  • 70,465
Ova
  • 466
  • 2
  • 5