How to modify external environment variable from 2nd shell script

pts.
Tags:
Bash
Bourne
KSH
There are two shell scripts. First one creates variable as: "export VAR1=init_value;" OR "declare -x; VAR1=init_value;" Second script can read this variable and modifies the variable's value but after exiting the variable still keep original value. What is wrong?

Answer Wiki

Thanks. We'll let you know when a new response is added.

Which shell are you using (ksh, bash, etc.) and how are you calling the 2nd script from the 1st script?

Discuss This Question: 2  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • Smooth
    I'm using bash. Calling 2nd script from the 1st directly by name, like: #!/bin/bash # # this 1st script script1.sh export Var1=100; # OR declare -xi Var1; Var1=100; ... ... echo $Var1 script2.sh echo $Var1 ... EOF ------------------------ #!/bin/bash # # this 2nd script script2.sh ... ... echo $Var1 Var1=$((Var1-1)); or any other arithmetic operation echo $Var1 ... ... EOF
    0 pointsBadges:
    report
  • Nmarco
    The problem is that the second script runs in it's own subprocess. Therefore when the second script ends, the value for $Var1 will be the same as it was before calling the second script. To overcome this, you can "source" the second script. In bash, this is accomplished with a "." (period) Therefore, instead of: echo $Var1 script2.sh echo $Var1 try this: echo $Var1 . script2.sh echo $Var1
    0 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following