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: Greg Wooledge
Subject: Re: [Help-bash] Different methods of running scripts; subshells and execute privileges
Date: Mon, 24 Aug 2015 12:17:01 -0400
User-agent: Mutt/1.4.2.3i

On Mon, Aug 24, 2015 at 08:44:16AM -0700, Michael Convey wrote:
> > Subshells may be created explicitly by putting ( ) around a command,
> > or more commonly
> 
> So is it possible to run a script in a true subshell? If so, is the
> following the only way to to do this?
> 
> ???$ ???
> bash (/path/script)
> ???$ ???
> sh (/path/script)
> ???$ ???
> /path/script()
> ???

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.



reply via email to

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