Penguin

Unix/Linux allows any character in a file/directory name except / or NUL. However, embedded spaces, newlines and the like can cause problems for carelessly-written Shell scripts (or scripts for carelessly-written shells), and cause some command PipeLines to fail completely.

To get around this, the GNU versions of find(1) and xargs(1) incorporate special extensions that work together. The -print0 option to find causes it to output matching pathnames terminated by NUL characters rather than newlines, thereby avoiding confusion with any embedded newlines that might occur in the pathnames. Correspondingly, the xargs command takes the option --null (shorter synonym -0) to indicate that its input is delimited in this way; though on my SUSE system I also have to specify --max-lines=1 (shorter synonym -L1).

For instance, suppose I have the following files
Events/Weddings/Freda's Wedding/data.txt
Events/Car Race/data.txt
Events/New Year Fireworks/data.txt
and I want to count up the number of lines in each "data.txt" file. I can do this as follows
find Events -name data.txt -print0 | xargs -0 -L1 wc -l

As discussed on the BashNotes page, scripts can cope with arguments that contain spaces if you are careful. bash will expand $@ as all of its arguments, but importantly will expand "$@" as each argument surrounded by "quote marks". Eg, if a script is called with arguments

 ./myscript "argument 1" "argument 2"

then a script that did

  perl -e 'foreach $a (@ARGV) {print ".$a.\n"}' $@

would output

.argument.
.1.
.argument.
.2.

while a script that did

  perl -e 'foreach $a (@ARGV) {print ".$a.\n"}' "$@"

would output

.argument 1.
.argument 2.