Tell me more ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How do I get the MD5 hash of a string directly from the terminal?

For example, I want the string abcdefg hashed. Currently the md5sum command only accepts a filename as input. I want to simply enter the following line and everything be done with.

md5sum abcdefg
output: ac54bcf346e578feb46888b3ecd2344f

How can I achieve that?

share|improve this question

4 Answers

up vote 17 down vote accepted

You can also say something like this :

~$ echo -n Welcome | md5sum
7803ffcaea43bb81a439fde13b29bc35  -

It basically does the same thing as described by @enzotib, but is perhaps a bit simpler.

share|improve this answer
7  
You should add the -n flag to echo unless you want the newline included in the value being md5summed. – David Jul 20 '11 at 12:49
Yep. you're right. – jfmessier Aug 1 '11 at 14:32

Very simple, it accepts stdin, so

md5sum <<<"my string"

To avoid the trailing newline added by the shell:

printf '%s' "my string" | md5sum
share|improve this answer
ahhh you saved me! thanks a zillion! – Hamed Momeni Jul 20 '11 at 11:14
@Hamed: if you are satisfied with the answer, you have the option to give it a vote, or better you can accept the answer (or both). – enzotib Jul 20 '11 at 11:19
1  
yeah I know. I was waiting for the time limit! ;) – Hamed Momeni Jul 20 '11 at 11:36
Giving both @messier and @enzotib a vote; both fall in my prized "elegant simplicity" category. I'd be apt to use the <<<" pipe in a script; echo string wins for the commandline. Well done. – Tom Jul 20 '11 at 17:01
1  
Yes, it does, as I said between first and second example – enzotib Jul 21 '11 at 18:32
show 3 more comments

Running md5sum with no arguments at all will cause it to read input from the terminal. Type or paste whatever you want, and when you are done, press ctrl-d to end the input.

share|improve this answer
yeah, you're right too. but ctrl+d needs to be pressed twice for it to work. – Hamed Momeni Sep 30 '11 at 13:57
@James, if it does not follow a newline, yes. If you hit it after hitting enter, it only needs once. When it does not follow a newline, it just forces all of the characters typed on the line so far to be processed immediately instead of waiting for a newline. – psusi Sep 30 '11 at 14:42

Among the 3 examples, one does not give the same result :

$ md5sum <<<"my string"
b9bfa87a6a126911f2246c7a615bff27  -

Other ones are correct and the right result:

2ba81a47c5512d9e23c435c1f29373cb  -

Now, let's summarize 4 examples :

$ md5sum <<<"my string"
b9bfa87a6a126911f2246c7a615bff27  -

$ md5sum <<< `echo -n "my string"`
b9bfa87a6a126911f2246c7a615bff27  -

$ printf '%s' "my string" | md5sum
2ba81a47c5512d9e23c435c1f29373cb  -

$ echo -n "my string" | md5sum
2ba81a47c5512d9e23c435c1f29373cb  -
share|improve this answer
Please, this is not an answer. Not even I can understand what you are trying to say. Make a comment on the question or on some answer, if you feel so, or ask a new question. – enzotib Jul 27 '11 at 13:38
Also, the problem of the newline in <<<"string" has been already pointed out – enzotib Jul 27 '11 at 13:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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