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

Why is Fish's psub so simple and effective compared to other shells? #2867

Closed
da99 opened this issue Mar 29, 2016 · 7 comments
Closed

Why is Fish's psub so simple and effective compared to other shells? #2867

da99 opened this issue Mar 29, 2016 · 7 comments
Labels

Comments

@da99
Copy link

da99 commented Mar 29, 2016

The psub function works great for my needs. The implementation is simple and quite elegant.

Fish's psub essence from: https://github.com/fish-shell/fish-shell/blob/master/share/functions/psub.fish#L49

cat >/tmp/pipe.fish &
echo /tmp/pipe.fish
#     diff   (echo abc | psub )  (echo def | psub)

However, a non-Fish equivalent would not work. This would block in other POSIX-y shells:

diff   $(echo abc | fishy.psub.sh )  $(echo def | fish.psub.sh)
# blocks until you write to the named pipe

How does Fish get away with implementing process substitution like that?

@da99 da99 changed the title Why does Fish's psub not work in other shells? How is Fish's psub so simple and effective compared to other shells? Mar 29, 2016
@da99 da99 changed the title How is Fish's psub so simple and effective compared to other shells? Why is Fish's psub so simple and effective compared to other shells? Mar 29, 2016
@faho faho added the question label Mar 29, 2016
@zanchey
Copy link
Member

zanchey commented Mar 30, 2016

it is a mystery

I suspect it would be helpful if we could see the implementation of fish.psub.sh, as the FIFO implementation in fish doesn't actually a) work or b) get effectively used (see #1040).

@da99
Copy link
Author

da99 commented Mar 30, 2016

In fishy.psub.sh, it would be a direct translation of Fish's psub function:

  my_pipe=path/to/temp/file
  mkfifo $my_pipe
  cat > $my_pipe &
  echo $my_pipe

But, in a non-Fish shell, this would just block until output was sent to the named pipe from somewhere else. In Fish, "it just works". (But, as @zanchey pointed out, it's not perfect.)

@da99
Copy link
Author

da99 commented Apr 2, 2016

"it is a mystery" is good enough for me.

In case anyone is curious how Fish's process substitution can be implemented in other shells, there is this discussion (specific to mksh): http://unix.stackexchange.com/a/273227/42107

It's a lot trickier compared to Fish. One more way Fish stands out from the crowd.

@da99 da99 closed this as completed Apr 2, 2016
@zanchey
Copy link
Member

zanchey commented Apr 2, 2016

FYI, the mkfifo stuff in fish does not work as it blocks with large enough outputs - psub always uses a temporary file.

@da99
Copy link
Author

da99 commented Apr 3, 2016

@zanchey Thanks for correcting me. Apologies for making you repeat yourself.

When do the temp. files psub creates get erased from the system? Reboot?

@zanchey
Copy link
Member

zanchey commented Apr 3, 2016

After the pipe is closed - see the function $funcname --on-job-exit caller part of the psub function.

@da99
Copy link
Author

da99 commented Apr 3, 2016

Is this a unique feature to Fish? (on-job-exit)
I never encountered anything like that on other shells.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants