Skip to content

Process substitution, the other way around #1786

Open

Description

@lloeki

diff (sort a.txt|psub) (sort b.txt|psub) is the most well known example of process substitution, but it's supposed to work the other way around too.

Bash:

$ xxd -l 20 -p /dev/random | tee >(cat 1>&2) | tr '[:lower:]' '[:upper:]'
2f441071086cabff98063780d1cc168803aadeb2
2F441071086CABFF98063780D1CC168803AADEB2

Fish:

> xxd -l 20 -p /dev/random | tee (psub | cat 1>&2) | tr '[:lower:]' '[:upper:]'
/tmp/.psub.cKChjevJJ7
A36ACA436120802CAF07AD859880BD8F03E76A5B

Ouch.

Activity

  1. added this to the fish-future milestone on Nov 1, 2014
  2. metal3d commented on Jul 12, 2015

    @metal3d

    Exactly what I need 👍

  3. metal3d commented on Jul 12, 2015

    @metal3d

    Precision, an example of what I need:

    With bash

    #!/bin/bash
    
    exec 3> >(zenity --progress)
    
    for i in $(seq 0 10 100); do
        sleep 1
        echo "hey $i"
        echo $i >&3
    done
    

    With fish

    #!env fish
    
    exec 3> (psub | zenity --progress)
    
    for i in (seq 0 10 100)
        sleep 1
        echo "hey $i"
        echo $i >&3
    end
    

    That doesn't work at all... Or maybe I'm doing it wrong.

  4. zanchey commented on Jul 12, 2015

    @zanchey
    Member

    No, it doesn't work yet.

  5. metal3d commented on Jul 12, 2015

    @metal3d

    yeah, the problem is pointed on the 3>

    env LANG=en fish z.fish                                                                                                                                  16:03:57
    fish: Encountered redirection when expecting a command name. Fish does not allow a redirection operation before a command.
    /tmp/z.fish (line 3): exec 3> (psub | zenity --progress)
                               ^
    fish: Illegal command name '(psub | zenity --progress)'
    /tmp/z.fish (line 3): exec 3> (psub | zenity --progress)
                                  ^
    fish: Error while reading file /tmp/z.fish
    

    Waiting for that feature :)

  6. rocketraman commented on Mar 7, 2016

    @rocketraman
    Contributor
  7. lordlycastle commented on Mar 30, 2016

    @lordlycastle
    Contributor

    Another example: keeping standard output on the screen as well as piping it to the next command.

    This works:

    ~ ) echo 'foo' | tee (tty) | tr '[:lower:]' '[:upper:]'
    foo
    FOO
    

    At least on OS X.

  8. metal3d commented on Jun 3, 2016

    @metal3d

    @lordlycastle it works on Linux also, thanks

  9. xealits commented on Jul 14, 2016

    @xealits

    A useful example of >(...) from stackexchange, redirection to multiple pipes/pipelines:

    seq 10 | tee >(rev > out1) >(tac > out2) >(shuf > out3)
    
  10. Perlence commented on Nov 22, 2017

    @Perlence

    Maybe something like this will do?

    seq 10 | tee (in-psub -o out1 -- rev) (in-psub -o out2 -- tac) (in-psub -o out3 -- shuf)

    So in-psub creates a named pipe $filename, outputs its name, and does cat $filename | rev > out1.

  11. mathieucaroff commented on Apr 23, 2018

    @mathieucaroff

    How do we deal with pipes inside process substitution?

    seq 15 | tee  >(rev | tac > out1) >(shuf > out2 2> err2)

    Maybe:

    seq 15 | tee (pipe (psub rev) (psub tac) > out1) (pipe (psub shuf) > out2 ^ err2)

    Though, that's a lot of parentheses and a lot of extra words.
    Maybe, building on Perlence idea of a -o option, we could shorten it a little:

    seq 15 | tee (pipe (psub rev) (psub -o out1 -- tac)) (psub -o out2 -e err2 -- shuf)
  12. RaitoBezarius commented on Sep 22, 2019

    @RaitoBezarius

    Any plan to support this feature or workaround? Thank you in advance!

  13. narun4sk commented on Feb 17, 2020

    @narun4sk

    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:

    kubectl apply -f <(istioctl kube-inject -f samples/httpbin/httpbin.yaml)
    

    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.

  14. zanchey commented on Feb 18, 2020

    @zanchey
    Member

    @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.

  15. 25 remaining items

  16. faho commented on Jul 27, 2023

    @faho
    Member

    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?

    I.e. something like this example from https://tldp.org/LDP/abs/html/process-sub.html:

    gzip | tee >(md5sum - | sed 's/-$/mydata.lz2/'>mydata-gz.md5) >mydata.gz
  17. nathansgreen commented on Aug 3, 2023

    @nathansgreen

    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.

  18. vergenzt commented on Jan 13, 2025

    @vergenzt
    1. comment from @simonrouse9461:

      The following syntax should be achievable: cat infile | tee (psub 'cat > outfile1') (psub 'tac > outfile2')

    2. comment from @r3a1d3a1:

      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:

    cat infile | tee (psub -i cat > outfile1) (psub -i tac | some-other-command | foo > outfile2)

    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).

  19. vergenzt commented on Jan 13, 2025

    @vergenzt

    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?

  20. vergenzt commented on Jan 13, 2025

    @vergenzt

    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...)

  21. mathieucaroff commented on Jan 13, 2025

    @mathieucaroff

    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).

    Bash:

    seq 15 | tee  >(rev | tac > out1.txt) >(shuf > out2.txt 2> err2.txt)

    Fish:

    seq 15 | tee (| rev | tac > out1.txt) (| shuf > out2.txt ^ err2.txt)

    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.

  22. miki725 commented on Jan 13, 2025

    @miki725

    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:

    # echo <(echo 'foo: bar')
    /dev/fd/63

    vs in fish you can pass a suffix:

    ➜ echo (echo 'foo: bar' | psub -s .yaml)
    /tmp/.psub.jdNXtDcsbA/psub.yaml
    
    ➜ prettier (echo 'foo: bar' | psub -s .yaml)
    foo: bar

    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:

    ➜ seq 15 | tee (| rev)
    # vs
    ➜ seq 15 | tee (psub -s .txt | rev)

    Although I dont think custom args will be necessary very often with write substitution.

  23. added a commit that references this issue on Jan 14, 2025
  24. PatrickHaecker commented on Mar 15, 2025

    @PatrickHaecker

    Fish:

    seq 15 | tee (| rev | tac > out1.txt) (| shuf > out2.txt ^ err2.txt)

    I think this syntax is intuitive. But to be consistent, too, I would then expect

    diff (a.txt | sort |) (b.txt | sort |)

    to be identical to

    diff (a.txt | sort | psub) (b.txt | sort | psub)

    This would not obsolete psub as it still has advantages when psub takes options.

  25. mathieucaroff commented on Mar 15, 2025

    @mathieucaroff

    Excellent idea PatrickHaecker! I love it!

  26. kaukas commented on May 24, 2025

    @kaukas

    Would this work if you need to pass arguments?

    seq 15 | tee (|(psub -s .txt) | rev)

    I understand it looks complicated, and might not compose very well…

  27. eira-fransham commented on Aug 27, 2025

    @eira-fransham

    Would this work if you need to pass arguments?

    seq 15 | tee (|(psub -s .txt) | rev)

    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 | begin tee (begin psub -s .txt >&3 | rev; end 3>&1 1>&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.

  28. Darkshadow2 commented on Aug 31, 2025

    @Darkshadow2
    Contributor

    (command substitution)[index] does actually work:

    > echo (seq 1 2 8)
    1 3 5 7
    > echo (seq 1 2 8)[1]
    1
    > echo (seq 1 2 8)[3]
    5
    > echo (seq 1 2 8)[4]
    7
  29. eira-fransham commented on Sep 2, 2025

    @eira-fransham

    @Darkshadow2 Fair, but it doesn't related to fds, so we still need new syntax.

  30. aparatext commented on Apr 11, 2026

    @aparatext

    Has there been any progress on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions