In one of my bash scripts I have this variable, containing a list of directories to be excluded from tar, plus their parent directory to be processed:
EXLIST="\
--exclude='/data/sub1/*' \
--exclude='/data/sub2/*' \
--exclude='/data/sub3/*' \
/data \
"
echo ${EXLIST} | /usr/bin/xargs -0 tar -cf _data.tar
However, tar cowardly refuses to create an empty archive because what it really receives after substituting ${EXLIST} is:
echo --exclude='/data/sub1/*' | /usr/bin/xargs -0 tar -cf /home/_data.tar
Which tells me that newlines are getting in the way?
I could of course define EXLIST as one long line but I prefer not to, as this makes the list less readable.
Is there a way, in bash, to “flatten” the lines to a string, so that tar can process it?
You should use an array in this case, without xargs.
Using this method, each element of the array is a separate argument for tar.