I am trying to obtain the md5 hash of a string. Said string consists of only uppercase and lowercase letters and digits. No other characters, including spaces and newlines. The string is inputted via a keyboard, rather than a file input, so there's no BOM or other flags present.

If I run locally using md5sum I get the following output:

ubuntu@ubuntu:~$ echo word | md5sum 
a46ec67a0f2e7c387926ac5d783ea4b8 - 

ubuntu@ubuntu:~$ echo "word" | md5sum
a46ec67a0f2e7c387926ac5d783ea4b8  - 

ubuntu@ubuntu:~$ echo "word" -n | md5sum
3a21ca21563e9535a9012daef61a0bdf  -

I know the difference between the 2nd and 3rd outputs above is due to the absence of the newline character (-n). This part is clear to me. (Turns out it was not! See the accepted answer.)

However, there are tools that give a third result. All non-CLI tools seems to give the same result, and that is what confuses me:

What's going on here?

$ echo "word" -n | md5sum
3a21ca21563e9535a9012daef61a0bdf  -
$ echo "word -n" | md5sum
3a21ca21563e9535a9012daef61a0bdf  -
$ echo "-n" word | md5sum
c47d187067c6cf953245f128b5fde62a  -

Specifically, note that:

$ echo "word" -n
word -n

All options must come before non-option arguments. This is the case in most programs except some GNU utilities, and is the case in all implementation of echo (echo is special however in that most strings beginning with - are interpreted as arguments, only a few options are recognized as such).

  • 1
    A better example might be to just show the output of echo "word" -n. Pretty clear why its failing when you see that! – derobert Jan 22 '15 at 21:53

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.