bash,md5sum 的行为很奇怪

use*_*089 2 bash md5 md5sum

为什么这是不同的?

text="tralala"
echo -n $text | md5sum -
Run Code Online (Sandbox Code Playgroud)

结果:def7d827536761c20f449f69262ff20f

echo -n "tralala" | md5sum -
Run Code Online (Sandbox Code Playgroud)

结果:7e4ef92d1472fa1a2d41b2d3c1d2b77a

我究竟做错了什么?

Emi*_*l H 5

我怀疑你错误地没有提供-n(输出无换行符)标志来回显。请参阅下面我的机器的示例:

$ echo tralala | md5sum
def7d827536761c20f449f69262ff20f  -

$ echo -n tralala | md5sum 
7e4ef92d1472fa1a2d41b2d3c1d2b77a  -

$ text="tralala"
$ echo  $text | md5sum 
def7d827536761c20f449f69262ff20f  -

$ echo -n $text | md5sum 
7e4ef92d1472fa1a2d41b2d3c1d2b77a  -
Run Code Online (Sandbox Code Playgroud)

  • 删除尾随的`-`、`echo -n tralala | md5sum | awk '{print $1}'` 应该可以解决问题 (2认同)