Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #16503

Re: ‘command … &’ creates subshell

From Eli Schwartz <eschwartz@archlinux.org>
Newsgroups gnu.bash.bug
Subject Re: ‘command … &’ creates subshell
Date 2020-06-30 17:29 -0400
Message-ID <mailman.728.1593552564.2574.bug-bash@gnu.org> (permalink)
References <pn9hipci.dag@gnui.org> <ac4a98bd-32f4-5c55-3b8f-1306e9758f54@case.edu> <7dvoi92p.dag@gnui.org> <09ea84b2-4fb1-6b05-7c0b-fe6b2ab00c6f@archlinux.org>

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

On 6/30/20 4:37 PM, Dmitry Alexandrov wrote:
> Chet Ramey <chet.ramey@case.edu> wrote:
>> On 6/29/20 4:33 PM, Dmitry Alexandrov wrote:
>>> 1) unexpected, as (a) it does not seem to be documented, (b) itʼs counter-intuitive and (c) other bourne-like shells (busybox ash, dash, ksh93, mksh, zsh) does not do that;
>>
>> It's an opportunity for optimization, not a bug
> 
> I am not saying that this is a bug, only that it is inconsistent with almost any other bourne-like shell.

And I understood Chet's point to be that you shouldn't have "expected"
something which isn't guaranteed. Just because it works out that way,
doesn't mean it has to; it's not like other shells document this behavior.

Relying on undocumented behavior isn't the greatest. :p

>>> 2) inconvenient, as in some cases PID of the parent shell is not an appropriate substitute for the PID of the real process.
> 
>> since the results are correct.
> 
> The results are _different_ across shell and, unfortunately, results provided by Bash are the least useful.  Consider:
> 
> 	$ command … &
> 	[1] 30737
> 	$ strace -p $!
> 	strace: Process 30737 attached
>         wait4(-1,
> 
> :-/

Useful replacements:

You could decline to use "command", and it doesn't involve a shell process.

$ cat &
[1] 2092238
$ ps --pid $! --ppid $!
    PID TTY          TIME CMD
2092238 pts/42   00:00:00 cat

[1]+  Stopped                 cat


If you need to ensure a disk executable is used,

$ "$(type -P cat)" &
[1] 2092352
$ ps --pid $! --ppid $!
    PID TTY          TIME CMD
2092352 pts/42   00:00:00 cat

[1]+  Stopped                 "$(type -P cat)"


Anyway, Chet didn't say it won't be changed to do what you expected;
after all, it's acknowledged as an opportunity for optimization, and
optimization is good..

-- 
Eli Schwartz
Arch Linux Bug Wrangler and Trusted User

Back to gnu.bash.bug | Previous | Next | Find similar


Thread

Re: ‘command … &’ creates subshell Eli Schwartz <eschwartz@archlinux.org> - 2020-06-30 17:29 -0400

csiph-web