14

How does one indicate that one has finished entering test in stdin?

For example, let's say that I wish to encrypt 'blue' using MD5 (I know MD5 is unsecure, but just for this example). I tried

user$ blue | md5

which I was led to understand is how one pipes input to stdin, but it doesn't work right.

But if I just enter

user$ md5

I can enter the word 'blue'. But how do I indicate to md5 that I'm finished entering text?

quack quixote
  • 42,186
  • 14
  • 105
  • 129
waiwai933
  • 2,483
  • 7
  • 28
  • 44
  • 2
    Your first attempt probably did not work correctly because you were trying to execute `blue` as a command instead of echoing it. Try `echo blue | md5` instead. – Trey Hunner May 12 '10 at 08:28

2 Answers2

19

are you talking about getting an md5sum for a piece of text?

if so run the md5sum command

type your text, when finished move to a new line by pressing return

press CTRL-D to end your input.

 user$ md5sum
 blue
 CTRL-D
bryan
  • 8,400
  • 4
  • 28
  • 41
  • 7
    This is the right answer. Ctrl-D is the canonical way to terminate keyboard stdin in any shell command. But strangely, I have an /sbin/md5 -- of indeterminate origin, but probably from OpenSSL -- on my system (Mac OS X v10.6.3) that doesn't terminate on the first Ctrl-D. It takes a second Ctrl-D to terminate it. Same with `openssl md5`. – Spiff May 12 '10 at 05:29
  • 4
    @Spiff: You should only need two ^D if you want to omit the final newline. If you can accept (or need) the final newline, then a single ^D on a fresh line should suffice. The EOF character (^D) flushes buffered input. If there is no buffered input, the process reading from the tty gets a zero length read (i.e. EOF). See (e.g.) [POSIX XRAT 11.1.9, EOF](http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap11.html#tag_11_01_09); [VEOF in Linux termios](http://linux.die.net/man/3/termios); or [EOF in FreeBSD termios](http://www.freebsd.org/cgi/man.cgi?query=termios&sektion=4). – Chris Johnsen May 12 '10 at 07:57
  • On WSL using `openssl md5` I have to press Ctrl-D 3 times. By 2030 we should have to press it 4 times at this rate. – Robert Feb 18 '21 at 18:23
0

In your first example, you need an echo:

user$ echo "blue" | md5
Dennis Williamson
  • 106,229
  • 19
  • 167
  • 187
  • it's useless. Idea was, all input is pressed by keyboard. Problem was how to mark stream end. – Znik Feb 25 '14 at 09:47