简体   繁体   中英

Set environment variables using ./ instead of eval

I have a script version.sh :

echo export VERSION="version-1.3"

I use this in another bash script test.sh :

eval "$(version.sh)"
echo $VERSION

The above code works and prints the version correctly.

However, I do not want to use eval . Is there a way to set environment variables and use them outside of another bash script without using eval ? For example, could I just use ./version.sh ?

You can do this:

version.sh:

export VERSION="version-1.3"

test.sh:

. version.sh
echo $VERSION

The dot in . version.sh . version.sh is the same as the command source version.sh . What it does is it executes commands from the file in the current shell. It doesnt run a new shell as in ./version.sh .

More info here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM