That's because you are using an unquoted ${1}
.
Quoting
Solving quoting issues could get quite complex.
Replacing '${1}'
with '"${1}"'
might seem to help.
Compare:
$ set -- "ab cd"; bash -c 'printf "<%s> " '${1}' "fd" "$0"'
<ab>
with:
$ set -- "ab cd"; bash -c 'printf "<%s> " '"${1}"' "fd" "$0"'
<ab> <cd> <fd> <bash>
However, shell "quote removal" is still applied to the variable value.
As a workaround you could use '"${1@Q}"'
$ set -- 'a"b c"d'; bash -c 'printf "<%s> " '"${1}"' "fd" "$0"'; echo
<ab cd> <fd> <bash> # quotes got lost.
$ set -- 'a"bc"d'; bash -c 'printf "<%s> " '"${1@Q}"' "fd" "$0"'; echo
<a"b c"d> <fd> <bash> # correct quotes.
But, still, that doesn't work for the two loops of shell exposure that your command has (first to the find
command, then to the bash -c
command):
$ mkdir 'a"bc"d' 'a"b c"d' 'a"bcd'
$ set -- 'a"bc"d'; find "./$1" -type d -exec bash -c 'printf "<%s> " fixperm "'"${1}"'" "fd" "$0"' {} >
<fixperm> <abcd> <fd> <./a"b c"d>
$ set -- 'a"b c"d'; find "./$1" -type d -exec bash -c 'printf "<%s> " fixperm "'"${1}"'" "fd" "$0"' {} >
<fixperm> <ab> <cd> <fd> <./a"b c"d>
$ set -- 'a"bcd'; find "./$1" -type d -exec bash -c 'printf "<%s> " fixperm "'"${1}"'" "fd" "$0"' {} ;
./a"bcd: -c: line 0: unexpected EOF while looking for matching `"'
./a"bcd: -c: line 1: syntax error: unexpected end of file
Correct
However, what really happens is that there seems to be a confusion between the $1
that is a parameter of the script you call and what $1
means to the shell that is being called with bash -c
The line:
find "/mnt/Data/Shared/$1" -type d -exec bash -c '
fixperm "'"${1}"'" "fd" "$0"' {} \;
Should read:
find "/mnt/Data/Shared/$1" -type d -exec bash -c '
fixperm "$1" "fd" "$2"' bash-shell "$1" {} \;
Which makes the quoting direct and a lot more robust.
Simple
If there is no loop or other complex function to run inside the bash -c
script, almost all quoting could be removed and write:
dir="/mnt/Data/Shared"
find "$dir/$1" -type d -exec fixperm "$1" fd {} ;