从实验位,它看起来像--quoting-style=escape
是与被包裹在$'...'
兼容,具有两个例外:
- 它通过预置一个反斜杠来转义空格;但
$'...'
不会在空格前放弃反斜杠。
- 它不会脱离单引号。
所以,你也许可以写这样的事情(Bash中):
function ls-quote-shell() {
ls -Q --quoting-style=escape "[email protected]" \
| while IFS= read -r filename ; do
filename="${filename//'\ '/ }" # unescape spaces
filename="${filename//"'"/\'}" # escape single-quotes
printf "$'%s'\n" "$filename"
done
}
为了测试这一点,我创建了一堆怪异字符的文件名的目录;和
eval ls -l $(ls-quote-shell)
按预期工作。 。 。尽管我不会对此做出任何坚定的保证。
另外,这里是一个使用printf
处理逃逸,随后printf %q
重新逃生的壳友好的方式的一个版本:
function ls-quote-shell() {
ls -Q --quoting-style=escape "[email protected]" \
| while IFS= read -r escaped_filename ; do
escaped_filename="${escaped_filename//'\ '/ }" # unescape spaces
escaped_filename="${escaped_filename//'%'/%%}" # escape percent signs
# note: need to save in variable, rather than using command
# substitution, because command substitution strips trailing newlines:
printf -v filename "$escaped_filename"
printf '%q\n' "$filename"
done
}
但如果事实证明,有一些情况下是第一个版本没有正确处理,那么第二个版本很可能会有相同的问题。 (FWIW,eval ls -l $(ls-quote-shell)
按照既定版本的预期工作)。
'--quoting-style =“escape”'或'--quoting-style =“c”'应该可以工作...... –
@ l'L'l:不。 'eval ls $(ls -Q --quoting-style = escape)'产生'ls:不能访问anb:没有这样的文件或目录',所以它不会往返。如果没有'eval',它也不起作用。 – choroba
'c'可能是你最好的选择......任何时候你都可以在一个子壳中评估它会得到奇怪的结果。 –