Although I've read myself through most of the questions about handling spaces in path names, I haven't found my case yet, which is about several path names in one variable.
find
will need this when two base paths are given, for example:
pa="/home/user/folder1 /home/user/folder2"
find $pa -name '*xy*' ...
This will look for matches in both folder1
and folder2
.
(Please note that this is a simplified example; I know very well that in this special case, you could use the -path
modifier with a parameter like folder*
that spans both folders. However, this question is to cover cases where you cannot work around the issue so easily due to higher complexity.)
Now imagine folder2
contains a space in the middle. The following will not work:
pa="/home/user/folder1 /home/user/fol\ der2"
find $pa -name '*xy*' ...
The problem is the \
, which is normally used to represent a space in the shell but will not work when the variable content is enclosed in double quotes.
However, unlike with one path per variable (where it might do the trick), the following won't work either:
pa=/home/user/folder1 /home/user/fol\ der2
find $pa -name '*xy*' ...
$ ./test.sh: line 3: /home/user/fol der2: Is a directory
nor will the following variant:
pa=/home/user/folder1 /home/user/fol\ der2
find "$pa" -name '*xy*' ...
So how can spaces be handled properly in paths if several paths are contained in one variable?
Related question: Is there any reliable way at all to determine whether a whitespace is a separator between one path and its successor (within the same variable), or a whitespace that belongs to one of the paths contained within?