我想遍歷文件列表而不關心文件名可能包含的字符,因此我使用由空字符分隔的列表。代碼將更好地解釋事情。通過Bash循環讀空分隔的字符串
# Set IFS to the null character to hopefully change the for..in
# delimiter from the space character (sadly does not appear to work).
IFS=$'\0'
# Get null delimited list of files
filelist="`find /some/path -type f -print0`"
# Iterate through list of files
for file in $filelist ; do
# Arbitrary operations on $file here
done
以下代碼在從文件讀取時有效,但需要從包含文本的變量中讀取。
while read -d $'\0' line ; do
# Code here
done < /path/to/inputfile
我不認爲有可能將空字符存儲在bash變量中。至少,我從來沒有找到辦法做到這一點...... – 2011-12-30 18:25:20