Powershell: Implement the use of `&` to background a command

Created on 21 Mar 2016  ·  11Comments  ·  Source: PowerShell/PowerShell

So we should implement it.

Issue-Enhancement Resolution-Fixed Size-MultipleWeeks WG-Language

Most helpful comment

This has been merged #3360

All 11 comments

Like Bash's background job ampersand, or PowerShell's call operator ampersand? I thought we had the latter.

like Bash's; Today the token '&' at the end of a line results in the following:
At line:1 char:4

  • ls &
  • ~
    The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks ("&") to pass it as part of a string.

    • CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException

    • FullyQualifiedErrorId : AmpersandNotAllowed

However, I think that '&' on its own is not as interesting as real job control. It would be nice to take this opportunity to implement _that_ :)

Ah, this isn't even supported in full PowerShell. Gotcha.

Fish-shell doesn't have this capability.
Related issue in their repo https://github.com/fish-shell/fish-shell/issues/563

Summary of UX sync discussion:

  • The complete solution Ctrl+Z, job control (fg/bg support) is an uncosted work item and can't be done by MVP (even though we'd absolutely like to do it eventually). @BrucePay is going to cost the full work.
  • The full solution won't work on Windows (no SIGSTOP/SIGHUP).
  • If the uncosted "full solution" can't be done by MVP, one option is to just add some syntactic sugar that invokes Start-Process when the & is caught. This would also work on Windows
  • Another option is to change the current error thrown when a trailing & is used to tell the user to use Start-Process/start. This would also work on Windows.

In the event that the uncosted work is too expensive, would either of these alternatives be acceptable for an MVP?

FWIW the latest insiders build of VS Code forks and execs properly, so it doesn't block the shell.

Hah... I remember a couple of years ago trying to see how this kind of thing could be hacked into v2 - it's a little tricky it seems to be consistent about this given that cmdlets are inproc and stuff like the gnu utils run out of process. That's not the say there might not be a away to manage both styles of commands with a trailing ampersand.

What I took out of my tinkering was that to support emulation of bg/fg, you'd have to implement a way to detach busy runspaces from the host UI, effectively letting them buffer into their streams collections (debug, verbose, warning, error, output etc.) until reattached. Wrap detached runspaces up as Jobs, and allow enumeration of them via a jobsourceadapter. The unix equivalent command for showing bg procs is "jobs" which also could alias to get-job. A detached runspace might host a native binary (out of proc) or an inproc (cmdlet) running in a pipeline and when I say native binary, I'm generally thinking about text-emitting commands rather than GUI-mode binaries (which I think on windows powershell should always run asynchronously by default.)

With respect to on-demand suspending with ctrl+z, I think this could be emulated with a global hook on windows or some kind of watchdog thread, but I think due to the lack of sighup, windows bg'd runspaces would not suspend, but instead go straight into the background. I think this is a tolerable difference. Thread.Suspend is obsolete and non-deterministic/passive anyway from a job-control perspective - you've no idea when a child thread might be ready to yield.

I'd imagine that the trailing ampersand token may well just be handled with a parser hack or some extension to psreadline's line parsing, but the latter wouldn't handle ampersands in scripts, but it might be a good first stab for prototyping (e.g. only support interactive fg/fg/&)

Apart from seeming a bit hacky, are there real, practical downsides to implementing & in fish using bash as a subshell spawner?

function background --description "Run a fish command in the background using a bash subshell"
    bash -c "fish -c '""$argv""' &"
end

background "sleep 5; echo hi"

This background implementation appears to have exactly the behavior I'd expect from native &.

@pirate yes; the process launched by "background" will not have access to unexported variables or user-defined functions in fish. Same would apply to a PowerShell implementation that worked this way.

user@user /> function background
                        bash -c "fish -c '""$argv""' &"
                      end

user@user /> function sayhello
                        echo hello
                      end

user@user /> sayhello
hello
user@user /> background sayhello
... fish: Unknown command “sayhello”
Standard input: sayhello

This has been merged #3360

Looking forward to trying this out!

Sent from my Windows 10 phone

From: Bruce Payettenotifications@github.com
Sent: Friday, May 19, 2017 4:16 PM
To: PowerShell/PowerShellPowerShell@noreply.github.com
Cc: Oisin Grehanoising@gmail.com; Commentcomment@noreply.github.com
Subject: Re: [PowerShell/PowerShell] Implement the use of & to background a command (#716)

This has been merged #3360https://github.com/PowerShell/PowerShell/pull/3360


You are receiving this because you commented.
Reply to this email directly, view it on GitHubhttps://github.com/PowerShell/PowerShell/issues/716#issuecomment-302801021, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ABwjIVzZi9ul-Ls7_tHLVu5fCgGIsI7sks5r7fidgaJpZM4H1PoD.

Was this page helpful?
0 / 5 - 0 ratings