字符串在这里添加换行符 [英] Here string adds line break

查看:506
本文介绍了字符串在这里添加换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎here string正在添加换行符.有删除它的便捷方法吗?

It seems that here string is adding line break. Is there a convenient way of removing it?

$ string='test'
$ echo -n $string | md5sum
098f6bcd4621d373cade4e832627b4f6  -
$ echo $string | md5sum
d8e8fca2dc0f896fd7cb4cb0031ba249  -
$ md5sum <<<"$string"
d8e8fca2dc0f896fd7cb4cb0031ba249  -

推荐答案

是的,您是对的:<<<添加了尾随新行.

Yes, you are right: <<< adds a trailing new line.

您可以通过以下方式查看它:

You can see it with:

$ cat - <<< "hello" | od -c
0000000   h   e   l   l   o  \n
0000006

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

Let's compare this with the other approaches:

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

来自为什么bash此处字符串会在末尾添加换行符?:

大多数命令要求输入文本.在Unix世界中,

Most commands expect text input. In the unix world, a text file consists of a sequence of lines, each ending in a newline. 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.

这篇关于字符串在这里添加换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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