我想遍历一个文件列表,而不用关心文件名可能包含的任何字符,因此我使用了以空字符为分隔符的列表。代码会更好地解释这些。
# 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旨在用于posix派生环境,在这些环境中,环境变量在以空字符结尾的缓冲区中内部存储,并且bash变量是(在我检查过的每种情况下)主机环境变量。 - memthatest=$'a\011b';echo ${#test} ="${test}"=
的结果为3 =a b=
。然后尝试一个八进制0:test=$'a\0b';echo ${#test} ="${test}"=
的结果为1 =a=
;这报告了$test的零终止字符串长度为1,但'b'和另一个零仍然可以存储到变量中,我们不知道。 - db-inf