Another Way to Pass Variables to a Subshell
If you want to send the value of a variable to a subshell, there’s another way to do it besides setting the variable and then exporting it. On the command line, you can precede the name of the command with the assignment of as many variables as you want. For example,
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">DBHOME=/uxn2/data DBID=452 dbrun</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
places the variables DBHOME and DBID, and their indicated values, into the environment of dbrun and then dbrun gets executed. These variables will not be known to the current shell; they’re created only for the execution of dbrun. In fact, execution of the preceding command behaves identically to typing
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">(DBHOME=/uxn2/data; DBID=452; export DBHOME DBID; dbrun)</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
Here’s a short example:
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$ cat foo1</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">echo :$x:</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">foo2</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$ cat foo2</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">echo :$x:</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$ foo1</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">::</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">:: x not known to foo1 or foo2</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$ x=100 foo1 Try it this way</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">:100: x is known to foo1</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">:100: and to its subshells</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$ echo :$x:</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">:: Still not known to current shell</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;">$</span>
<span style="color: #000000; font-family: tahoma, arial, helvetica, sans-serif;"> </span>
So variables defined this way otherwise behave as normal exported variables to the subshell.