Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process substitution, the other way around #1786

Open
lloeki opened this Issue Oct 31, 2014 · 10 comments

Comments

Projects
None yet
9 participants
@lloeki
Copy link

commented Oct 31, 2014

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.

@metal3d

This comment has been minimized.

Copy link

commented Jul 12, 2015

Exactly what I need 👍

@metal3d

This comment has been minimized.

Copy link

commented Jul 12, 2015

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.

@zanchey

This comment has been minimized.

Copy link
Member

commented Jul 12, 2015

No, it doesn't work yet.

@metal3d

This comment has been minimized.

Copy link

commented Jul 12, 2015

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

@rocketraman

This comment has been minimized.

Copy link

commented 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

This comment has been minimized.

Copy link
Contributor

commented Mar 30, 2016

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.

@metal3d

This comment has been minimized.

Copy link

commented Jun 3, 2016

@lordlycastle it works on Linux also, thanks

@xealits

This comment has been minimized.

Copy link

commented Jul 14, 2016

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

seq 10 | tee >(rev > out1) >(tac > out2) >(shuf > out3)
@Perlence

This comment has been minimized.

Copy link

commented Nov 22, 2017

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.

@mathieucaroff

This comment has been minimized.

Copy link

commented Apr 23, 2018

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.