help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Different methods of running scripts; subshells and exec


From: Michael Convey
Subject: Re: [Help-bash] Different methods of running scripts; subshells and execute privileges
Date: Mon, 24 Aug 2015 09:55:00 -0700

On Mon, Aug 24, 2015 at 9:17 AM, Greg Wooledge <address@hidden> wrote:

> Parentheses have to go around the ENTIRE command.  You can't stick them
> in the middle.
>
> Here are some examples of explicit subshells:
>
> (cd /foo && make)
> (cd /bar && make clean)
> (unset DISPLAY; /some/terminal-or-X/editor)
> (shopt -s dotglob nullglob; files=(*); echo "There are address@hidden files
> here.")
>
> What do these all have in common?  There is a global side effect that
> we DON'T want.  So we do the job in a subshell, and the global side
> effect (changing directory, unsetting environment variables, changing
> shell options) goes away.  That's the purpose of an explicit subshell.
>
> There is no reason to wrap "sh /path/to/script" in an explicit subshell.
> It's already going to fork a new process.  If you were to do it anyway,
> you would have to put the parentheses around the ENTIRE command:
>
> (sh /path/to/script)
>
> But again, it's silly.  It accomplishes nothing.
>

​I see. I really appreciate you hanging with me on this. My understanding
really improves with each of your replies and I'm very grateful.

Most sources on the web and in books say the following creates a subshell:

sh /path/to/script​

But you are saying it doesn't create a true subshell and now I understand
why. But the script above does fork() and exec() a different shell. What is
the correct nomenclature for that shell? If not subshell, then what? A
"Child Shell"? Also, would there ever be a legitimate reason to run a
script in a true subshell? Perhaps via the following?

(source /path/to/script)

The preceding would fork() a true subshell and run the script therein,
right?


reply via email to

[Prev in Thread] Current Thread [Next in Thread]