Karthik
Karthik

Reputation: 824

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

Upvotes: 4

Views: 18869

Answers (7)

Vimukt
Vimukt

Reputation: 1

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

Upvotes: 0

Peter
Peter

Reputation: 1

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.

Upvotes: 0

Lesmana
Lesmana

Reputation: 27073

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

Upvotes: 3

evil otto
evil otto

Reputation: 10582

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.

Upvotes: 2

jlliagre
jlliagre

Reputation: 30873

script2.sh:

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

Upvotes: 1

c00kiemon5ter
c00kiemon5ter

Reputation: 17674

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.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799520

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

Upvotes: 1

Related Questions