BayerSe
BayerSe

Reputation: 1171

Correct bash syntax for path with space and comma

I am using rsync to backup data. Now I have troubles backing up a folder that contains a comma and a space character. I use an array with source names:

dirs=(
    /home/user/Desktop
    ...
    /home/user/foo, bar
)

What is the correct syntax for the last entry?

Upvotes: 0

Views: 699

Answers (1)

Tom Fenech
Tom Fenech

Reputation: 74695

Just quote them:

dirs=(
    '/home/user/Desktop'
    ...
    '/home/user/foo, bar'
)

This prevents word splitting from occurring. As these are string literals (no expansion of variables, for example), I prefer to use single quotes.

Presumably you plan on looping through these directories, in which case, you should also quote the variable inside the loop:

for dir in "${dirs[@]}"; do
    rsync command using "$dir"
done

Double quotes are needed here so that word splitting isn't performed but the variable is still expanded.

Upvotes: 5

Related Questions