【发布时间】:2012-01-30 10:38:12
【问题描述】:
我想遍历文件列表而不关心文件名可能包含哪些字符,因此我使用了一个由空字符分隔的列表。代码会更好地解释事情。
# 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 变量中。至少,我从来没有找到一种方法来做到这一点......
-
已确认,
bash: warning: command substitution: ignored null byte in input。这是因为 bash 旨在用于 posix 衍生环境,其中 env var 在内部存储在以 null 结尾的缓冲区中,而 bash var(在我检查过的每种情况下)都是宿主 env var。
标签: bash delimiter null-character