-print0
should not be used in a $(...)
substitution, because strings in bash variables are null-terminated.
I asked a question whose answer was similar to what this question requires: https://stackoverflow.com/a/30469553/1091693
Adapting that answer to your question:
to_dump=()
while IFS= read -r -d ''; do
to_dump+=( "$REPLY" )
done < <(find . -maxdepth 1 -print0)
This creates an array called to_dump
and uses the read
command to read NULL-delimited elements from find
. The reason < <(...)
is being used here rather than a pipe is to avoid an implicit subshell which would prevent the array from being modified.
It's worth noting that your original find
command probably wants a -mindepth 1
, or it will pick .
(the current directory) and you'll end up doing a recursive copy on that.
I've noticed you use -maxdepth 1
as an argument to find, so perhaps this will be more useful:
shopt -s nullglob
to_dump=( * .[!.]* ..?* )
Avoiding find
, this uses bash builtins only, doesn't fork, and is for the most part quite clean.
The first line, shopt -s nullglob
, is a bash(-only) command which turns on the nullglob
option. This option is described in man 1 bash
:
If set, bash allows patterns which match no files (see Pathname Expansion above) to expand to a null string, rather than themselves.
In simpler terms, if you type *
but it doesn't match files, it will remove the *
. The default behaviour is to put the *
in there anyway.
The second line adds 3 globs to the array:
*
: All files not beginning with .
.[!.]*
: All files beginning with one .
and one non-.
character. This is to avoid matching the .
and ..
directories.
..?*
: All files beginning with ..
and at least one more character. Added for the same reason as the previous glob, covering the cases it missed.
Bash expands the globs into the definition of the array, and it expands them correctly -- no splitting on whitespace or anything like that.
A caveat on the usage of nullglob: If you have nullglob turned on, curl google.com/search?q=test
will result in curl complaining at you for not passing it arguments, and ls /var/fasdfasafs*
will give you a listing of the current directory. This is one of the reasons it's not turned on by default.