0

How can I add the output of a string in MD5 to a file, for example add the MD5 of Hello, world! to a file /home/documents/helloworld.txt

2 Answers 2

3

If you don't want to hash a trailing newline, use:

printf '%s' 'string' | md5sum >outfile

Most often, one computes a hash, such as an MD5 sum, of the entire contents of a file. You can compute the hash of any string, but there is a twist: many methods of producing a string append a trailing newline character.

Thus, with the newline:

$ echo 'Hello, World!' | md5sum
bea8252ff4e80f41719ea13cdf007273  -
$ md5sum <<<'Hello, World!'
bea8252ff4e80f41719ea13cdf007273  -
$ md5sum <<'EOF'
Hello, World!
EOF
bea8252ff4e80f41719ea13cdf007273  -
$ printf '%s\n' 'Hello, World!' | md5sum
bea8252ff4e80f41719ea13cdf007273  -

Without the newline:

$ printf '%s' 'Hello, World!' | md5sum
65a8e27d8879283831b664bd8b7f0ad4  -
$ echo -n 'Hello, World!' | md5sum
65a8e27d8879283831b664bd8b7f0ad4  -

In all of those methods, you can redirect the output of md5sum such as by writing >outfile next to it to write to outfile instead of your terminal. Depending on your goals, any of them may be reasonable. However, if your goal is to compute the MD5 hash of Hello, World! (or whatever your string is), rather than Hello, World! with a newline appended, then you must not use any of the methods that append a newline.

As shown, echo without -n, here strings, here documents, and printf with \n produce a newline, while printf without \n and echo with -n leave it off. Note that, if your string doesn't contain any % or \ characters (which printf treats specially in its first argument), you can even write:

$ printf 'Hello, World!' | md5sum
65a8e27d8879283831b664bd8b7f0ad4  -

Note that printf is generally preferable to echo.


Finally, for interactive use, there's another way to do it:

  1. Run md5sum (or md5sum >outfile to redirect to outfile).
  2. Enter your string.
  3. If the string you entered didn't end in a newline, press Ctrl+D. This flushes the buffer even though you're in the middle of a line and the buffer is (probably) not full.
  4. Press Ctrl+D again. This flushes the buffer, but it is empty, so md5sum reads zero bytes. This is the "end of file" condition, so md5sum stops reading and gives a result.

This is simpler and faster in practice than I've made it sound. It works because terminals treat Ctrl+D specially. (In the rare event you wanted to send that character literally as part of your string, you could press Ctrl+V followed immediately by Ctrl+D.)

If your string ends with a newline, then skip step 3 as the buffer has already been flushed. That is, if the last key you pressed was Enter (or if you pasted text in and that text ends in a newline, so you are at the beginning of a line) press Ctrl+D just once instead of twice.

Note that, subject to the caveat that you must actually be entering the string into your terminal, this method is fully general. Your string can have other newlines in it. For example, you could type md5sum, then Enter, then hello, then Enter, then world, then Ctrl+D twice, and this gives the same result as printf 'hello\nworld' | md5sum.

1

Echo the text and pipe to md5sum. Redirect the output to a file:

echo "Hello, World!" | md5sum > "/home/user/documents/helloworld.txt"

EDIT: As @EliahKagan points out, this includes a trailing newline. See his excellent answer for a better solution.

2
  • 2
    Note that this computes the MD5 hash of the 14-character string consisting of Hello, World! followed by a newline character, not of the 13-character string Hello, World!. Jul 16, 2020 at 6:56
  • That is an excellent point, nice answer.
    – Dan Scally
    Jul 16, 2020 at 7:00

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.