“回波”和“回波”之间的区别是什么?和“echo -n”? [英] What is the difference between "echo" and "echo -n"?

查看:269
本文介绍了“回波”和“回波”之间的区别是什么?和“echo -n”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

echo -n 终端上的手册页如下:

 不打印结尾的换行符。这也可以通过将`\c'附加到字符串的末尾来实现
,如通过
iBCS2兼容系统所做的那样。注意,此选项以及`\c'的
效果是在由Cor。修订的IEEE Std 1003.1-2001
(`POSIX.1')中实现定义的。 1-2002。强烈鼓励旨在
最大可移植性的应用程序使用printf(1)至
抑制换行符。

一些shell可能提供一个内建的echo命令,这个命令与这个实用程序类似或者相同。最值得注意的是,sh(1)中的内置回声不会
接受-n选项。请参阅builtin(1)手册页。

当我尝试通过以下方式生成MD5哈希:

  echopassword| md5 

它返回 286755fad04869ca523320acce0dc6a4



当我做

  echo -npassword
/ pre>

它返回在线MD5生成器返回的值: 5f4dcc3b5aa765d61d8327deb882cf99



选项 -n 有什么区别?

解决方案

当您执行 echopassword| md5 echo 向要进行哈希处理的字符串添加换行符,即 password\\\
。当添加 -n 开关时,它不会,所以只有字符 password 被哈希。



更好地使用 printf ,它不需要任何开关就可以实现:

  printf'password'| md5 

对于'password' isn

  printf'%s'$ pass| md5 

这意味着密码中的转义字符(例如 \\\
\t )不会解释为 printf p>

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 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.

这篇关于“回波”和“回波”之间的区别是什么?和“echo -n”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆