在将sed更新为4.4版本后,sed不会在作为here-string提供的find命令的输出中用逗号替换空格:
sed --version
sed (GNU sed) 4.4
ls -l /tmp/test/
total 0
-rw-r--r-- 1 root root 0 Jan 9 17:25 a
-rw-r--r-- 1 root root 0 Jan 9 17:25 b
# NOT EXPECTED
sed "s: :,:g" <<< $(find /tmp/test/ -type f)
/tmp/test/b
/tmp/test/a在sed 4.2中没有问题
sed --version
sed (GNU sed) 4.2.2
ls -l /tmp/test/
total 0
-rw-r--r-- 1 root root 0 Jan 9 17:25 a
-rw-r--r-- 1 root root 0 Jan 9 17:25 b
# as expected
sed "s: :,:g" <<< $(find /tmp/test/ -type f)
/tmp/test/a,/tmp/test/b作为一种解决方法,将结果存储在变量中并使用echo有助于:
a=$(find /tmp/test/ -type f)
echo $a | sed "s: :,:g"
/tmp/test/b,/tmp/test/a如何在sed 4.4中使用here-string实现相同的输出?
<#>更新
bash的版本在这两个系统之间也发生了变化:
bash --version
GNU bash, version 4.4.20旧版本
bash --version
GNU bash, version 4.3.48发布于 2022-01-09 17:57:56
这是一个介于变化版本4.3和4.4之间的bash
Bash不再像文档中常说的那样,拆分这里字符串的扩展。
正确的行为是您的新版本,因为您依赖于旧代码中的bug。
这会给你一个逗号分隔的文件列表,
find -type f | tr '\n' , | sed 's/,$/\n/'但是,由于文件名本身可以包含新行和逗号,所以很容易破坏这种脆弱的代码。如果你愿意分享你的处理-在一个新的问题-我相信有人会推荐一个更好的方式处理文件名可靠和安全。
https://unix.stackexchange.com/questions/685678
复制相似问题