in zsh, when the option multios is enabled, I can pipe output to two different processes, ie:

cat /dev/sda > sda.img | sha256sum > sda.img.sha256

How could I achieve the same in dash, ie /bin/sh ?


You can use tee to copy the stream to a file and pipe the result over to let sha256sum read from standard input and write it to a separate file. The read from standard input designator - is optional though

</dev/sda tee sda.img | sha256sum - > sda.img.sha256

which of course should work on any minimal shell like dash.

It's actually more efficient here than using zsh's multios as the data goes through fewer pipes. Here, it's read once by tee, once by sha256sum and written twice by tee while with the multios approach, it's read once by cat, once by the zsh's internal teeing process (through a pipe), once by sha256sum and written once by cat, twice by the internal teeing process.

On ksh or bash where process substitution(>(..)) is supported like in zsh, you could use that

< /dev/sda tee >(sha256sum > sda.img.sha256) > sda.img
  • 1
    Note that the process substitution approach has no advantage whatsoever over the pipe one, only drawbacks (here that sha256sum is not waited for with some shells, and as a result sda.img.sha256 may not be populated yet by the time you run the next command). Jul 17 '20 at 7:31
  • @StéphaneChazelas: Thanks Stephane, as ever for your valuable feedback
    – Inian
    Jul 17 '20 at 7:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.