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
Share:
8,363

Related videos on Youtube

mans
Author by

mans

Updated on September 18, 2022

Comments

  • mans
    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?