0

test.sh:

#! /bin/sh
me=I ./test2.sh

test2.sh:

#! /bin/sh
echo $me

run script 1 and printing this:

[zhibin@szrnd1 sh]$ ./test.sh
I
[zhibin@szrnd1 sh]$ 

As see, the variable "$me" be transferred to "test2.sh".

I didn't find this usage of variable definition on googling, can someone tell me where can find the tutorial including the usage above mentioned?

Thx a lot!

2
  • in addition to answers below, you should read stackoverflow.com/questions/10938483/… Commented Aug 8, 2014 at 3:25
  • Thanks to All of you tell me these so quickly! The manual is really detailed :)) Commented Aug 8, 2014 at 3:50

2 Answers 2

2

Since this has been mentioned on SO a lot, I'm assuming you're looking for some documentation on it. I'm not sure there is anything more detailed than the BASH documentation about this:

The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described in Shell Parameters. These assignment statements affect only the environment seen by that command.

As you've seen by experimenting, when you do "A=B command", it runs the command as if "export A=B" was run just prior to that command then A's value reverts to its previous after command completes. It's a very convenient way to pass some environment into a command while ensuring the rest of the script is not affected.

Sign up to request clarification or add additional context in comments.

Comments

2

From the bash documentation on the Environment:

The environment for any simple command or function may be augmented temporarily by prefixing it with parameter assignments, as described in Shell Parameters. These assignment statements affect only the environment seen by that command.

So if you put variable assignments before a command, that command is run with those environment variables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.