Skip to main content
1 of 3
z32a7ul
  • 445
  • 4
  • 13

Bash quoted variable fails unquoted works

I read about that I should quote variables in bash, e.g. "$foo" instead of $foo. However, while writing a script, I found an a case where it works without quotes but not with them:

wget_options='--mirror --no-host-directories'
local_root="$1" # ./testdir recieved from command line
remote_root="$2" # ftp://XXX recieved from command line 
relative_path="$3" # /XXX received from command line

This one works:

wget $wget_options --directory_prefix="$local_root" "$remote_root$relative_path"

This one does not (note the double quotes aroung $wget_options):

wget "$wget_options" --directory_prefix="$local_root" "$remote_root$relative_path"
  • What is the reason for this?

  • Is the first line the good version; or should I suspect that there is a hidden error somewhere that causes this behavior?

  • In general, where do I find good documentation to understand how bash and its quoting works? During writing this script I feel that I started to work on a trial-and-error base instead of understanding the rules.

z32a7ul
  • 445
  • 4
  • 13