2 ms·
And how do you safely iterate over the output of "find" (including properly handling file names beginning with dashes, while you're at it), without forking anot
by DonHopkins 8y ago
And how do you safely iterate over the output of "find" (including properly handling file names beginning with dashes, while you're at it), without forking another sub-bash for each file name?
- pdkl95 8y agoAs I said, that hasn't been a problem for a long time: $ find . -type f ./foo bar ./--stupid-filename ./-annoying dir name!/--very annoying path... $ echo "pid=$$" pid=31281 $ while IFS= read -r -d '' file ; do > echo "[pid=$$] do something with '${file}'" > list[i++]="$file" > done < <(find . -type f -print0) [pid=31281] do something with './foo bar' [pid=31281] do something with './--stupid-filename' [pid=31281] do something with './-annoying dir name!/--very annoying path...' $ declare -p list declare -a list='([0]="./foo bar" [1]="./--stupid-filename" \ [2]="./-annoying dir name!/--very annoying path..." [3]="./iter.sh")' Using the character literal $'\0' also works as the delimiter, but Bash doesn't support sending null bytes in args. In bash, $'\0' is the empty string: $ x=$'\0' $ declare -p x declare -- x=""