Defining a script shell variable on the same line as searching for another script

I encounter lines in ksh scripts that declare a variable and create another shell script on the same line. For instance:

MYVARIABLE="abc" . ~core/script.sh 

Does this mean that the behavior is different from declaring them separately? For instance:

 MYVARIABLE="abc" . ~core/script.sh 
+4
source share
1 answer

When

 MYVARIABLE="abc" . ~core/script.sh 

MYVARIABLE set to abc only for the duration of the source ( . ) Command. This way it will be available for code in ~core/script.sh .

When

 MYVARIABLE="abc" . ~core/script.sh 

it will be installed both in the region of the current shell / script and in the region of ~core/script.sh .

Try starting first, then echo $MYVARIABLE to see that it is still empty (if it is running empty). Run the last one, echo again, and you will see that its value is set in the current shell.

Edit:

Note that MYVARIABLE displayed in the ~core/script.sh in both cases here, but only because you are using a script. If you were to execute an executable file (even an executable shell script) instead, rather than its source, the behavior would be different.

 MYVARIABLE=foo /usr/local/bin/some_executable 

will have a variable in scope at runtime, but

 MYVARIABLE=foo /usr/local/bin/some_executable 

will not have a scope variable. In order for it to spread to the executable / subshell, you need to export it:

 MYVARIABLE=foo export MYVARIABLE /use/local/bin/some_executable 

In Bourne-derived shells newer than ksh (bash, zsh), you can combine destination and export:

 export MYVARIABLE=foo 

but this does not work in ksh; you need two statements.

+6
source

All Articles