The manual page on Terminal for echo -n is the following:
-n Do not print the trailing newline character. This may also be
achieved by appending `\c' to the end of the string, as is done by
iBCS2 compatible systems. Note that this option as well as the
effect of `\c' are implementation-defined in IEEE Std 1003.1-2001
(``POSIX.1'') as amended by Cor. 1-2002. Applications aiming for
maximum portability are strongly encouraged to use printf(1) to
suppress the newline character.
Some shells may provide a builtin echo command which is similar or iden-
tical to this utility. Most notably, the builtin echo in sh(1) does not
accept the -n option. Consult the builtin(1) manual page.
When I try to do generate an MD5 hash by:
echo "password" | md5
It returns 286755fad04869ca523320acce0dc6a4
When I do
echo -n "password"
It returns the value that online MD5 generators return: 5f4dcc3b5aa765d61d8327deb882cf99
What difference does the option -n do? I don't understand the entry in Terminal.
When you do
echo "password" | md5
,echo
adds a newline to the string to be hashed, i.e.password\n
. When you add the-n
switch, it doesn't, so only the characterspassword
are hashed.Better to use
printf
, which does what you tell it to without needing any switches:For cases where
'password'
isn't just a literal string, you should use a format specifier instead:This means that escape characters within the password (e.g.
\n
,\t
) aren't interpreted byprintf
and are printed literally.