when to know we are going to work in sub shell or would be entering sub shell
Guys,
I am being enlightened by one of the senior member from this forum that this is no requirement to do "export" for any variable unless it is going to be required in sub shell. So now my question(s) goes, How do we know that we are going to require a sub shell or we are entering(the variable) in fact is entering a sub shell. Also can anybody advice me , when do we use words like let, define, set ?? Thank you in advance. |
bash?
|
Actually, the assertion about subshells is somewhat inaccurate. All processes (not just shells) have an environment that contains a list of key/value pairs. All processes inherit the environment of their parent process when the parent fork()s. Bash and other Bourne shells have a similar key/value pairs list which can be manipulated, and behaves otherwise like an environment variable. When one or more of those variables is exported, it is moved to the environment that allows it to be inherited by any child process (not just subshell) that is spawned by the shell.
What I described above is kind of jargon for what happens when you set and export an environment variable before running a program. By default, X client applications need to have a $DISPLAY variable set: Code:
export DISPLAY=localhost:0 --- rod. |
A subshell is formed whenever you run a secondary command inside another command string with command substitution or process substitution. They are also created for the second and subsequent commands in a command list (i.e. a pipe chain), and when a command is forked into the background with "&". Subshells can also be created explicitly with the '(..)' grouping. There may be other cases that I can't remember off-hand.
In other words, it's a temporary environment forked off by the shell in which to do some work, generally while in the process of executing another command. See the bash man page. A sub-process, OTOH, is just what it says it is, an entirely new process thread created by and forked off from the parent. The sub-process runs pretty much independently, but is still a sub-node of its parent and will be terminated if the parent process terminates. In shell terms this usually refers to any external commands you run, although subshells are also a kind of sub-process. The big difference between a sub-process and a subshell is that the former only inherits values from the parent that have the export flag set, whereas a subshell is an almost exact duplicate of the parent, and all environment values are made available, whether exported or not. |
As a bit of addition...
You also get sub-shells (at least with most of them) when you use loop constructs such as while, for, do. The advantage is that it allows you to redirect input/output to/from the loops. For example: Code:
while read VAR ; do You also get sub-shells for certain grouping operations - using parentheses as in: Code:
(cd path; tar -cf - .) | (cd otherpath; tar -xf -) This example is a bit contrived (tar now has a -C option to do the same thing), but it used to be very common when copying directory trees around (especially when going from one system to another). |
Quote:
|
Ok. got that one wrong.
|
Perhaps you were thinking of this common gotcha:
Code:
cat file.txt | while read -r line; do This does work, however, since there are no subshells involved: Code:
while read -r line; do I set variables in a loop that's in a pipeline. Why do they disappear after the loop terminates? Or, why can't I pipe data to read? http://mywiki.wooledge.org/BashFAQ/024 |
Quote:
--- rod. |
All times are GMT -5. The time now is 08:27 AM. |