Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 97 down vote accepted

You can also say something like this :

~$ echo -n Welcome | md5sum
83218ac34c1834c26781fe4bde918ee4  -

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

share|improve this answer
You also want to update the example result, as 7803ffca... is the result with the added newline. The correct result for the command above is 83218ac34c1834c26781fe4bde918ee4 - – Xanthir May 27 '15 at 21:14
why are there spaces and a dash at the end? – keune Dec 2 '15 at 9:46
Please correct me if I'm wrong, but I think this is because the MD5sum was applied to a stream of data, as opposed to reading a file content, which has a name associated to it. – jfmessier Dec 3 '15 at 12:45
Notice that the -n is mandatory. Without it, your hash will be totally wrong since it includes the newline character. – Pithikos Feb 8 at 11:23

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
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 for using printf correctly. If you want to have the sum without the -, put | cut -d ' ' -f 1. Example usage: sum=$(printf '%s' 'some string' | md5sum | cut -d ' ' -f 1) – Lekensteyn Jul 21 '11 at 8:49
it's weird but the <<< operator and the printf function are giving completely different results for md5 hash! the result of printf is correct though! – Hamed Momeni Jul 21 '11 at 15:30
it seems using the <<< operator sends a newline to the md5sum! – Hamed Momeni Jul 21 '11 at 15:32
Yes, it does, as I said between first and second example – enzotib Jul 21 '11 at 18:32

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
$ echo -n 123456 | md5sum | awk '{print $1}'

you can create a shell script.

For example,the script name is


echo   -n   $1 | md5sum | awk '{print $1}'

permission execute:

 chmod +x


$ 123456
share|improve this answer
This is just too long. – Gucho Ca May 12 at 3:40

My quick poke at the --help for md5sum demonstrates that the command:

md5sum -

will then give a prompt for simple input. Inputting some text and then using Enter and then Ctrl+D to signify end of file then causes md5sum to spit out the MD5 of the raw text you entered.

Less to type and no piping! Woo!

If you do not hit Enter before Ctrl+D you will have to enter Ctrl+D twice, where the result is the wrong hash-sum.

share|improve this answer
openssl md5 filename
openssl sha1 filename

For string pipe the content using echo

echo -n 123456 | openssl md5
share|improve this answer

In my scripts I found that there are 2 things that you should know about this issue.

  • It does not matter if you do echo "$myvariable" or echo -n "$myvariable" but you should always use the doubleqoutes for strings and always use the same method. if not things won't match.
  • in the output you get always a trailing space and a dash as shown in the example:

    $ echo -n Welcome | md5sum
    7803ffcaea43bb81a439fde13b29bc35  -

to get rid of that and stay only with the code 7803ffcaea43bb81a439fde13b29bc35, do: echo "$myvariable" | md5sum | cut -d" " -f1

share|improve this answer
Don't forget the -n parameter here to avoid outputting the trailing newline, which would lead to a wrong md5: echo -n $myvariable | md5sum | cut -d" " -f1 – derFunk Dec 3 '15 at 19:28

protected by heemayl Feb 3 at 13:36

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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