0

On my Linux system, I want to generate a MD5 check sum and save it to a variable. The command to generate check sum is:

$ md5sum myfile.zip

And the output is:

060906cec6fd1037ff275d928f96b419  myfile.zip

I want to pipe this output into a read command like so:

$ md5sum myfile.zip | read sum filename

However, that does not seem to work: the variables sum and filename remains undefined. I worked around this problem by redirecting the output into a file, then read it back:

$ md5sum myfile.zip > tempfile
$ read sum filename < tempfile

What did I do wrong and how do I fix it? I appreciate your answers and comments.

Hai Vu
  • 37,849
  • 11
  • 66
  • 93
  • The *already answered* question does not address my question. The comments from Inian and M. Nejat Aydin do. If you create the answer, I'll upvote. – Hai Vu Jul 27 '20 at 18:46
  • 1
    It does address your question. The accepted answer shows why and how to use process substitution here, and even offers alternatives. – oguz ismail Jul 27 '20 at 18:48
  • 1
    I take back: it does addressed my question. Thank you, @oguzismail – Hai Vu Jul 27 '20 at 18:51
  • 1
    Try `read sum filename <<<$( md5sum myfile.zip )`. Here the read command is *not* executed in a subshell. – Wiimm Jul 27 '20 at 22:17
  • @Wiimm That also works. Thank you. – Hai Vu Jul 28 '20 at 19:10

0 Answers0