when to know we are going to work in sub shell or would be entering sub shell
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 ??
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
xterm
The child process (xterm) inherits the $DISPLAY variable, because it was exported.
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.
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
echo $VAR
done <filename
This example is from a procedure that used to be used during particular boot sequences on SunOS - it was termed a "shcat" (shell cat) because it accomplishes the same function as cat, but doesn't require a cat utility binary to work.
You also get sub-shells for certain grouping operations - using parentheses as in:
Code:
(cd path; tar -cf - .) | (cd otherpath; tar -xf -)
The parentheses here are used to group the context of two commands (the cd, and the tar). In this particular example, the two groups are tied together by sending the stdout of the first to the stdin of the second.
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).
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.
No, loop constructs don't create sub-shells (redirection doesn't require having a sub-shell).
cat file.txt | while read -r line; do
[[ $line == *foo* ]] && fooline=$line
done
echo "$fooline"
$fooline will never contain anything because the while loop runs inside a subshell (it's the second element in a pipe chain). And no sub-process can modify the environment of its parent.
This does work, however, since there are no subshells involved:
Code:
while read -r line; do
[[ $line == *foo* ]] && fooline=$line
done <file.txt
echo "$fooline"
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.