看来这里的字符串是添加换行符.有没有方便的删除方法?
$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.