Não tente criar uma string executável. Em vez disso, construa os argumentos em uma matriz e use isso ao chamar tar (você já está usando uma matriz corretamente para EXCLUDE ):
#!/bin/bash
directory=/tmp
exclude=( "hello hello" "systemd*" "Temp*" )
# Now build the list of "--exclude" options from the exclude array:
for elem in "${exclude[@]}"; do
exclude_opts+=( --exclude="$directory/$elem" )
done
# Run tar
tar -cz -f tmp.tar.gz "${exclude_opts[@]}" "$directory"
com /bin/sh :
#!/bin/sh
directory=/tmp
set -- "hello hello" "systemd*" "Temp*"
# Now build the list of "--exclude" options from the $@ array
# (overwriting the values in $@ while doing so)
for elem do
set -- "$@" --exclude="$directory/$elem"
shift
done
# Run tar
tar -cz -f tmp.tar.gz "$@" "$directory"
Observe as citações de $@ no código sh e de ambos os ${exclude[@]} e ${exclude_opts[@]} no código bash . Isso garante que as listas sejam expandidas para elementos citados individualmente.
Relacionados: