You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reacted by Łukasz Jan Niemier, Stefan Haller, Gary Peck, Carlos D'Agostino, Colin de Roos, Spencer Elliott, Ron Wolf, Xavier L'Heureux, Philippe Rigovanov, narun4sk and 40 more
If those long term POSIX compatible shell users (like myself) had a quick fix for the command substitution$() by just removing the $, in the fish shell there's no such luxury regarding process substitution.
@narun4sk, I have good news - back in 2006 the fish developer apparently had (ahem) 20/20 vision, and implemented the bit of process substitution you want in the psub command. You can run kubectl apply -f (istioctl kube-inject -f samples/httpbin/httpbin.yaml | psub) to get the effect you want.
This issue is talking about the other kind of process substitution, using the >() syntax.
We try not to use symbols where words will do. In fact we've reduced the number of special symbols that fish uses.
Symbols are hard to read and hard to find documentation for - googling e.g. fish >[ would be annoying, while googling "fish psub" or running help psub both work. And that's if you can even tell that the >[ is one construct.
And adding new symbols comes with the added cost that it's often backwards-incompatible.
Process substitution, especially this way around, is also simply not an important enough feature to warrant its own symbol. As one datapoint it's not in POSIX, so if you write a /bin/sh script you can't rely on it. I just confirmed that dash doesn't have it.
It's useful in some cases, but not extremely often, and so there's no need to give it the exalted position of having special symbols reserved for it.
Additionally: Your construct has no delimiters, so what happens if you want to have a pipe inside the process substitution, or anything else after?
As someone who has written a fair amount of POSIX code, I will say that lack of process substitution is bad for POSIX. For comparison, pipefail was only recently added to POSIX, and last I checked dash still hadn't added support for it. Lack of this feature makes reliable scripts much more difficult to write. Likewise, lack of process substitution results in a lot of bad compromises if you want scripts that behave reliably and fail predictably. Storing data in strings and printing it into pipes makes memory consumption highly unpredictable and can waste a lot of CPU if the next step fails. Using temp files results in unnecessary disk writes and the need to trap signals to clean up resources. (And, again, wasted CPU.) Working with fifos is likewise painful for setup and cleanup. When I want POSIX compatibility, I bypass fish completely.
Reacted by Tom, Michal Ulianko, 簡煒航 (Weihang Jian), David Adam, vindex10, repsac, Fahad Hossain, Thayne McCombs, hubert parzych, Tim Vergenz and 2 more
Quoted (sub)processes (as suggested [in comment above]) are an enemy of syntax highlighting.
I think @r3a1d3a1 makes a good point unfortunately. 🙁 IMO it'd be better than nothing (and is achievable in userland), but is not ideal for a first class addition to the shell.
I wish we didn't have to require quoting; it'd be nice to just be able to write the above example as:
but unfortunately I don't think that's possible in plain userland Fish (because if you pipe the psub command anywhere... then the filename output gets piped too).
I wonder if Fish maintainers would be open to a mechanism being added to core Fish to allow psub (and maybe other commands?) to be able to directly control what the result of a command substitution is from inside it separately from stdout, as opposed to always taking stdout (via exec_subshell_for_expand / expand_cmdsubst).
This would then enable the above flexible psub implementation; the psub function could call status set-command-substitution-output (or whatever we'd want the above to be called) with the filename instead of echoing it to stdout, and then "stdout" could be piped to other commands within the parenthetical subshell as usual.
It sounds complicated, but I think the user experience could be reasonable? 🤷 Next question IMO would then what should the scoping rules for "command substitution output assignment" be? Should such a complicated feature be accessible in userland, or should it be a special case for psub?
The other thought that occurs to me is that rather than making a whole "command substitution output assignment" feature and using that from a (userland-implemented) psub function, we could just have the Fish internals special-case psub directly.
(E.g. when evaluating a command substitution, if the first command in the pipeline is a call psub then take that command substitution's output from some unused higher file descriptor instead of fd1/stdout. Then modify function psub to output the filename to that higher file descriptor instead of to stdout? Idk, just spitballing here...)
Earlier, I identified that there are two kind of substitutions and since they are not named the Bash documentation, I proposed to name them as follows:
Readable process substitution for <(cmd) in bash, or (cmd | psub) in fish
Writable process substitution for >(cmd) in bash
To deal with pipes in fish, we could add a syntax where when an opening parenthesis is followed by a pipe character, fish triggers writable process substitution for the whole parenthesized command (possibly chaining more pipes).
That syntax is quite reminiscent of the way bash does writable process substitution, but I find the new syntax reads better and gives a stronger feel that the command is going to receive data.
Reacted by Miroslav Shubernetskiy, Thayne McCombs, vindex10, TANIGUCHI Kohei, Yegor, repsac, 汐 and Mikko
One aspect of psub in fish which is more flexible vs bash process substitution is that you can provide additional args to the psub. For example prettier needs to know the file extension in order to correctly figure out how to format a file.
In bash that does not work all the time as you cannot customize the extension:
To deal with pipes in fish, we could add a syntax where when an opening parenthesis is followed by a pipe character, fish triggers writable process substitution for the whole parenthesized command (possibly chaining more pipes).
This seems pretty user-friendly however that will loose the ability to pass additional args such as for a custom suffix:
I understand it looks complicated, and might not compose very well…
I'm not sure this works as psub is just a regular function and returns via stdout, so the semantics of (|(foo) | bar) would be "the outer command substitution returns the stdout of foo instead of bar, and the stdout of bar is printed to the shell stdout.
Tbh the main issue is that psub needs to use stdout to write its output, maybe there could be a way to specify that a command substitution should expand to the result of a file descriptor from the inner command other than stdout, with the other fds of the commands inside the substitution being forwarded like normal? My composability brain wants (..) to just return an array so seq 15 | tee (psub --fd 3 -s .txt | rev)[3] can be the syntax. However "unfortunately" (it was the right choice but it's unfortunate for this syntax proposal), fish has established the precedent that substituting an array without an index of any kind writes the entire array, rather than the first element like bash. So realistically it'd probably need some new syntax that's currently a syntax error like seq 15 | tee (psub --fd 3 -s .txt | rev)$&3 or possibly seq 15 | tee (psub --fd 3 -s .txt | rev)[&3].
I guess you could say that seq 15 | tee (psub --fd 3 -s .txt | rev)[&3] (or $&3, or whatever else) is kinda sugar for:
seq 15 |begintee (beginpsub-s .txt >&3 | rev; end 3>&11>&3); end 3>&1
Although you can't write the latter now as psub doesn't support stdin, plus I tried to redirect the fds of a block just now and that doesn't seem to work so that's another reason this requires changes in the shell itself rather than just in psub.
Activity
metal3d commentedon Jul 12, 2015
Exactly what I need 👍
metal3d commentedon Jul 12, 2015
Precision, an example of what I need:
With bash
With fish
That doesn't work at all... Or maybe I'm doing it wrong.
zanchey commentedon Jul 12, 2015
No, it doesn't work yet.
metal3d commentedon Jul 12, 2015
yeah, the problem is pointed on the
3>Waiting for that feature :)
rocketraman commentedon Mar 7, 2016
Another example: keeping standard output on the screen as well as piping it to the next command.
See http://stackoverflow.com/a/5677425/430128
lordlycastle commentedon Mar 30, 2016
This works:
At least on OS X.
metal3d commentedon Jun 3, 2016
@lordlycastle it works on Linux also, thanks
xealits commentedon Jul 14, 2016
A useful example of
>(...)from stackexchange, redirection to multiple pipes/pipelines:Perlence commentedon Nov 22, 2017
Maybe something like this will do?
So
in-psubcreates a named pipe$filename, outputs its name, and doescat $filename | rev > out1.mathieucaroff commentedon Apr 23, 2018
How do we deal with pipes inside process substitution?
Maybe:
Though, that's a lot of parentheses and a lot of extra words.
Maybe, building on Perlence idea of a
-ooption, we could shorten it a little:RaitoBezarius commentedon Sep 22, 2019
Any plan to support this feature or workaround? Thank you in advance!
narun4sk commentedon Feb 17, 2020
Just to keep this one open in 2020 and draw more attention to the problem I vouch for it too 🙂
It seems to me that Process substitution has been underestimated by the fish devs. In these days it's everywhere, for instance:
If those long term POSIX compatible shell users (like myself) had a quick fix for the command substitution
$()by just removing the$, in the fish shell there's no such luxury regarding process substitution.zanchey commentedon Feb 18, 2020
@narun4sk, I have good news - back in 2006 the fish developer apparently had (ahem) 20/20 vision, and implemented the bit of process substitution you want in the
psubcommand. You can runkubectl apply -f (istioctl kube-inject -f samples/httpbin/httpbin.yaml | psub)to get the effect you want.This issue is talking about the other kind of process substitution, using the
>()syntax.25 remaining items
faho commentedon Jul 27, 2023
We try not to use symbols where words will do. In fact we've reduced the number of special symbols that fish uses.
Symbols are hard to read and hard to find documentation for - googling e.g.
fish >[would be annoying, while googling "fish psub" or runninghelp psubboth work. And that's if you can even tell that the>[is one construct.And adding new symbols comes with the added cost that it's often backwards-incompatible.
Process substitution, especially this way around, is also simply not an important enough feature to warrant its own symbol. As one datapoint it's not in POSIX, so if you write a
/bin/shscript you can't rely on it. I just confirmed that dash doesn't have it.It's useful in some cases, but not extremely often, and so there's no need to give it the exalted position of having special symbols reserved for it.
Additionally: Your construct has no delimiters, so what happens if you want to have a pipe inside the process substitution, or anything else after?
I.e. something like this example from https://tldp.org/LDP/abs/html/process-sub.html:
nathansgreen commentedon Aug 3, 2023
As someone who has written a fair amount of POSIX code, I will say that lack of process substitution is bad for POSIX. For comparison,
pipefailwas only recently added to POSIX, and last I checkeddashstill hadn't added support for it. Lack of this feature makes reliable scripts much more difficult to write. Likewise, lack of process substitution results in a lot of bad compromises if you want scripts that behave reliably and fail predictably. Storing data in strings and printing it into pipes makes memory consumption highly unpredictable and can waste a lot of CPU if the next step fails. Using temp files results in unnecessary disk writes and the need to trap signals to clean up resources. (And, again, wasted CPU.) Working with fifos is likewise painful for setup and cleanup. When I want POSIX compatibility, I bypass fish completely.vergenzt commentedon Jan 13, 2025
comment from @simonrouse9461:
comment from @r3a1d3a1:
I think @r3a1d3a1 makes a good point unfortunately. 🙁 IMO it'd be better than nothing (and is achievable in userland), but is not ideal for a first class addition to the shell.
I wish we didn't have to require quoting; it'd be nice to just be able to write the above example as:
but unfortunately I don't think that's possible in plain userland Fish (because if you pipe the
psubcommand anywhere... then the filename output gets piped too).vergenzt commentedon Jan 13, 2025
I wonder if Fish maintainers would be open to a mechanism being added to core Fish to allow
psub(and maybe other commands?) to be able to directly control what the result of a command substitution is from inside it separately from stdout, as opposed to always taking stdout (viaexec_subshell_for_expand/expand_cmdsubst).This would then enable the above flexible
psubimplementation; thepsubfunction could callstatus set-command-substitution-output(or whatever we'd want the above to be called) with the filename instead of echoing it to stdout, and then "stdout" could be piped to other commands within the parenthetical subshell as usual.It sounds complicated, but I think the user experience could be reasonable? 🤷 Next question IMO would then what should the scoping rules for "command substitution output assignment" be? Should such a complicated feature be accessible in userland, or should it be a special case for
psub?vergenzt commentedon Jan 13, 2025
The other thought that occurs to me is that rather than making a whole "command substitution output assignment" feature and using that from a (userland-implemented)
psubfunction, we could just have the Fish internals special-casepsubdirectly.(E.g. when evaluating a command substitution, if the first command in the pipeline is a call
psubthen take that command substitution's output from some unused higher file descriptor instead of fd1/stdout. Then modifyfunction psubto output the filename to that higher file descriptor instead of to stdout? Idk, just spitballing here...)mathieucaroff commentedon Jan 13, 2025
Earlier, I identified that there are two kind of substitutions and since they are not named the Bash documentation, I proposed to name them as follows:
To deal with pipes in fish, we could add a syntax where when an opening parenthesis is followed by a pipe character, fish triggers writable process substitution for the whole parenthesized command (possibly chaining more pipes).
Bash:
Fish:
That syntax is quite reminiscent of the way bash does writable process substitution, but I find the new syntax reads better and gives a stronger feel that the command is going to receive data.
miki725 commentedon Jan 13, 2025
One aspect of
psubin fish which is more flexible vs bash process substitution is that you can provide additional args to thepsub. For exampleprettierneeds to know the file extension in order to correctly figure out how to format a file.In bash that does not work all the time as you cannot customize the extension:
# echo <(echo 'foo: bar') /dev/fd/63vs in fish you can pass a suffix:
This seems pretty user-friendly however that will loose the ability to pass additional args such as for a custom suffix:
Although I dont think custom args will be necessary very often with write substitution.
fish-shell#1786 experimentation
PatrickHaecker commentedon Mar 15, 2025
I think this syntax is intuitive. But to be consistent, too, I would then expect
to be identical to
This would not obsolete
psubas it still has advantages whenpsubtakes options.mathieucaroff commentedon Mar 15, 2025
Excellent idea PatrickHaecker! I love it!
kaukas commentedon May 24, 2025
Would this work if you need to pass arguments?
I understand it looks complicated, and might not compose very well…
eira-fransham commentedon Aug 27, 2025
I'm not sure this works as
psubis just a regular function and returns via stdout, so the semantics of(|(foo) | bar)would be "the outer command substitution returns the stdout of foo instead of bar, and the stdout of bar is printed to the shell stdout.Tbh the main issue is that
psubneeds to use stdout to write its output, maybe there could be a way to specify that a command substitution should expand to the result of a file descriptor from the inner command other than stdout, with the other fds of the commands inside the substitution being forwarded like normal? My composability brain wants(..)to just return an array soseq 15 | tee (psub --fd 3 -s .txt | rev)[3]can be the syntax. However "unfortunately" (it was the right choice but it's unfortunate for this syntax proposal),fishhas established the precedent that substituting an array without an index of any kind writes the entire array, rather than the first element likebash. So realistically it'd probably need some new syntax that's currently a syntax error likeseq 15 | tee (psub --fd 3 -s .txt | rev)$&3or possiblyseq 15 | tee (psub --fd 3 -s .txt | rev)[&3].I guess you could say that
seq 15 | tee (psub --fd 3 -s .txt | rev)[&3](or$&3, or whatever else) is kinda sugar for:Although you can't write the latter now as
psubdoesn't support stdin, plus I tried to redirect the fds of a block just now and that doesn't seem to work so that's another reason this requires changes in the shell itself rather than just inpsub.Darkshadow2 commentedon Aug 31, 2025
(command substitution)[index]does actually work:eira-fransham commentedon Sep 2, 2025
@Darkshadow2 Fair, but it doesn't related to fds, so we still need new syntax.
aparatext commentedon Apr 11, 2026
Has there been any progress on this?