2

We have a shell script named source.sh that exports many ENV variables required for a build.

I am trying to access those ENV variables by running source.sh in my script, but even after running that I am not able to access ENV variables for the succeeding commands.

My Script look alike:

#!/bin/bash

sh source.sh
cd $ROOT_VOS
make

But here cd $ROOT_VOSis not happening

Haris
  • 31
  • 3
  • As an aside, there's no good reason to be making this an environment variable vs a regular shell variable -- and POSIX conventions specify the namespace of all-uppercase variable names to be used for variables with meaning for the operating system and shell, with lowercase names reserved for application use. (Formally, that's specified for environment variables, but setting a regular shell variable overwrites any like-named environment variable, so the convention necessarily applies in both places). – Charles Duffy Dec 06 '16 at 16:37

1 Answers1

3

sh source.sh

...runs source.sh in a separate copy of sh (actually, since your parent shell is bash and the child is sh, it's not just a "separate copy", it's an entirely different interpreter). When that separate interpreter exits, local variables and other changes to process-local state are gone. (Environment variables are inherited by child processes; they aren't propagated up the tree to parents).


Instead, use:

# POSIX-compliant: execute every command in source.sh in the current shell
. source.sh

...or...

# bash-specific, perhaps more-readable, synonym for the same
source source.sh
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441