相关文章推荐
淡定的油条  ·  Visual Studio 2022 ...·  1 年前    · 
帅气的小熊猫  ·  Ubuntu ...·  1 年前    · 
寂寞的数据线  ·  jQuery 事件 | 菜鸟教程·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

You might also find the POSIX specification for echo at pubs.opengroup.org/onlinepubs/009604599/utilities/echo.html worth reading. Notably, it points out that echo -n is not portable across all UNIX systems (though this is better than echo -e, which cannot be implemented without breaking the standard outright!) – Charles Duffy Jun 10, 2015 at 16:31

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 characters password are hashed.

Better to use printf, which does what you tell it to without needing any switches:

printf 'password' | md5

For cases where 'password' isn't just a literal string, you should use a format specifier instead:

printf '%s' "$pass" | md5

This means that escape characters within the password (e.g. \n, \t) aren't interpreted by printf and are printed literally.

OK, that makes a lot more sense! I always thought first the string was hashed and then the newline was added; now I know it's the other way around. – user4820905 Jun 10, 2015 at 18:42 The ironic thing is now that the hashes I generate are harder to be cracked because cracking programs have to add a newline to every entry, so using echo is better in a way! ;) – user4820905 Jun 11, 2015 at 15:22 Do you know why break lines are ignored when comparing strings in bash, like this: [[ $(echo -e "a\n") = $(echo -n "a") ]] && echo yes why does that condition return true? – Edgar Magallon Jan 27 at 8:13 @EdgarMagallon all trailing newlines are removed from command substitutions, so those commands are both equivalent to a (try for yourself after running set -x in your shell). – Tom Fenech Jan 29 at 1:41

Output the args, separated by spaces, followed by a newline. (...) If -n is specified, the trailing newline is suppressed.

Having this into account, it is always safer to use printf, which provides the same functionality as echo -n. That is, no default new line is added:

$ echo "password" | md5sum
286755fad04869ca523320acce0dc6a4  -
$ echo -n "password" | md5sum
5f4dcc3b5aa765d61d8327deb882cf99  -
$ printf "%s" "password" | md5sum
5f4dcc3b5aa765d61d8327deb882cf99  -   # same result as echo -n

See the superb answer in Why is printf better than echo? for more info.

And another example:

$ echo "hello" > a
$ cat a
hello
$ echo -n "hello" > a
$ cat a
hello$            # the new line is not present, so the prompt follows last line
                I might suggest printf %s "password", to work reliably even if the password is being expanded from a variable and may contain backslash escapes or the like.
– Charles Duffy
                Jun 10, 2015 at 16:30
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.