Home > Software design >  Set environment variables using ./ instead of eval
Set environment variables using ./ instead of eval

Time:10-22

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?

CodePudding user response:

You can do this:

version.sh:

export VERSION="version-1.3"

test.sh:

. version.sh
echo $VERSION

The dot in . 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

  •  Tags:  
  • bash
  • Related