简体   繁体   中英

Passing one shell script variable to another shell script

I am trying to access one script variable from another script for example,

script1.sh:

export a=10
export b=20
echo "testing"
exit

script2.sh:

. script1.sh
echo $a

The problem involved here is its able to access the variable 'a' from the script1 in script2 but its executing all the commands written in script1.sh which is annoying. I just want to access only the exported variables in script1 and donot want to run the commands in script1 while calling that in script2 .

Kindly help!

Thanks,
Karthik

Assigning a variable is a command. Unless you're prepared to write a bash parser in bash, what you want cannot be done.

What you are trying to do (only get the variables from another script, without executing the commands) is not possible.

Here is a workaround:

Extract the variable declaration and initialisation to a third file, which is then sourced by both.

common-vars:

export a=10
export b=20

script1.sh:

. common-vars
echo "testing"
exit

script2.sh:

. common-vars
echo $a

If your variable assignments are only ever in the exact form indicated above, that is

export a=10

then you could extract those assignments with grep and eval the results. Your script2.sh would then be

eval `grep "^export " script1.sh`
echo $a

But doing this is very fragile, and can break in lots of ways. Putting the variable assignments in a third script sourced by both as others have suggested is far safer.

This can break if you have variable settings inside conditionals, or if your exported variables depend on other non-exported variables that got missed, or if you set and export the variables separately; and if you happen to be using command substitution in your exports those would still get run with their possible side effects.

if [ $doexport = 1 ]; then
  export a=1
fi

a=2
export a

b=2
export a=$b

export a=`ls |wc -l`

These are all potentially problematic.

I agree with "Ignacio Vazquez-Abrams" this is a good answer to your question.

But I have another solution for you where you don't have export your variables. concept is based on: use you Login shell to run both the script instead of a new shell.

Lets say your script1.sh is:

a=10 

b=20

now run your script in your login shell:

Example:

.[space]script1

Now if you will access the variable a in your script it will be accessible to you without fail.

Example

Lets say your script2.sh is:

echo $a

run:

.[space]script2

You can pass the variable from one script to another by passing the variable as a parameter. This of course means that Script B will be called by Script A. Script B will need to check that the parameter passed is not empty.

I agree with Ignacio Vazquez-Abrams.

Assigning a variable is a command

not much you can do about it.

A probable solution would be to seperate the variable definitions to a third script, that would be used as configuration script. So both script1 and script2 would source that one, to use the vars defined there.

script2.sh:

eval "$(grep export script1.sh)"
echo $a

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