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
  • 69,223
  • 56
  • 216
  • 327
mans
  • 171
  • 1
  • 1
  • 6
  • Take a look at [this answer](https://askubuntu.com/a/26411/18193) if you just want to set some environment variables without having to use a bash script. – devius Mar 27 '18 at 10:05
  • 2
    To make it work in the parent shell, you need to *source* the file rather than just executing it - see for example [In a bash script what does a dot followed by a space and then a path mean?](https://askubuntu.com/a/232938/178692) – steeldriver Mar 27 '18 at 10:18

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
  • 69,223
  • 56
  • 216
  • 327
Ova
  • 456
  • 2
  • 5