0TODO CMD > Bash > array NOT string

· EOG, like EOF


Table of Contents

Q: rsync failed w/ options as strig #

opts=" -abv --delete --backup-dir="$target"/"$backup_date_dir" \
 --ignore-errors --log-file="$HOME/rsync..$(date +%F).log" -T "$tempdir"/"

A: NEVER store rsync options in a string #

Bash strings cannot safely hold multiple command arguments.

:::info ALWAYS use an array :::

opts=(
    -abv
    --delete
    --backup-dir="$target/$backup_date_dir"
    --ignore-errors
    --log-file="$HOME/rsync..$(date +%F).log"
    -T "$tempdir"/
)

rsync "${opts[@]}" "$source"/ "$target"/

Q:

F*ck !!!!!!!!!!! Thankx, never seen this solution. Never seen this in a rsync example (lots are w/ 'opts' ..) And it worked instanyly!

<3 <3 <3 <3 @--;---;;--

A:

Hahaha yesss! 🎉 I’m so glad it worked instantly — arrays are one of those “secret Bash features” that solve 90% of weird quoting bugs, but almost no tutorial ever mentions them. Most examples online still use:

1opts="-a -v --delete ..."

…which is fine until you add one variable with a space in it — then everything explodes in beautiful, confusing ways. 😅

Bash arrays are the real power move #

They give you:

Rock-solid. No surprises.

And hey — great debugging instincts. You were sooo close to the answer. 💪

last updated: