從實驗位,它看起來像--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'可能是你最好的選擇......任何時候你都可以在一個子殼中評估它會得到奇怪的結果。 –