Bash: 文件名中包含 utf8 字符的 cat 文件

Bash: cat file with utf8 characters in filename

我正在编写自己的 bash 脚本,我在文件名中使用 UTF8 字符时遇到一些问题。

我的文件名称为:../ahh/Nový textový dokument.txt

我收到这个错误:

cat: '"../ahh/Nov35 textov35 dokument.txt"': No such file or directory

对于其他人脚本工作正常。

我的 bash 脚本片段:

git status -su | tr \r \n | while read -r line ;
do
    FILE=$(echo "$line" | awk '{ = ""; print substr([=12=],2)}')
    cat "$FILE"
done

如果你这样做 git statusgit ls-files 你可能会得到类似的东西:

$ git status 
A  "Nov35 textov35 dokument.txt"

这是因为默认情况下 git 以引用的八进制表示法打印非 ASCII 文件名,可以通过以下方式为当前存储库禁用此功能:

git config core.quotepath off

然后:

$ git status -s
A  "Nový textový dokument.txt"

查看此答案了解更多详情: