Cannot set an environment variable using a Bash script
8,363
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
Related videos on Youtube
Author by
mans
Updated on September 18, 2022Comments
-
mans over 1 year
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?
-
devius about 6 yearsTake a look at this answer if you just want to set some environment variables without having to use a bash script.
-
steeldriver about 6 yearsTo 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?
-