加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 这里的字符串添加换行符

发布时间:2020-12-15 18:23:42 所属栏目:安全 来源:网络整理
导读:看来这里的字符串是添加换行符.有没有方便的删除方法? $string='test'$echo -n $string | md5sum098f6bcd4621d373cade4e832627b4f6 -$echo $string | md5sumd8e8fca2dc0f896fd7cb4cb0031ba249 -$md5sum "$string"d8e8fca2dc0f896fd7cb4cb0031ba249 - 是的,
看来这里的字符串是添加换行符.有没有方便的删除方法?
$string='test'
$echo -n $string | md5sum
098f6bcd4621d373cade4e832627b4f6  -
$echo $string | md5sum
d8e8fca2dc0f896fd7cb4cb0031ba249  -
$md5sum <<<"$string"
d8e8fca2dc0f896fd7cb4cb0031ba249  -
是的,你是对的:<<<添加一个尾随的新行. 你可以看到它:
$cat - <<< "hello" | od -c
0000000   h   e   l   l   o  n
0000006

让我们将其与其他方法进行比较:

$echo "hello" | od -c
0000000   h   e   l   l   o  n
0000006
$echo -n "hello" | od -c
0000000   h   e   l   l   o
0000005
$printf "hello" | od -c
0000000   h   e   l   l   o
0000005

所以我们有桌子:

| adds new line |
-------------------------|
printf   |      No       |
echo -n  |      No       |
echo     |      Yes      |
<<<      |      Yes      |

从Why does a bash here-string add a trailing newline char?开始:

Most commands expect text input. In the unix world,07001.
So in most cases a final newline is required. An especially common
case is to grab the output of a command with a command susbtitution,
process it in some way,then pass it to another command. The command
substitution strips final newlines; <<< puts one back.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读