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!
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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:
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:
while read VAR ; do
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:
(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).