I encountered a very strange behavior when running below command, let me explain the issue:
Consider this simple bash script:
#!/bin/bash
zip -r /var/backup.zip /var/www -x /ignore_dir_1/\*
It compresses the whole www folder recursively and excludes ignore_dir_1 which is perfectly fine.
Now, write that script like this:
#!/bin/bash
Exclude="/ignore_dir_1/\*"
zip -r /var/backup.zip /var/www -x $Exclude
It runs without error but does not exclude ignore_dir_1.
Can anyone please explain this behavior?
- Disclaimer:
I have already tried the following alternatives:
Exclude="/ignore_dir_1/*"
Exclude="/ignore_dir_1/***"
Update:
Thanks to @pLumo, the problem solved by putting variable inside quotation like:
#!/bin/bash
Exclude="/ignore_dir_1/*"
zip -r /var/backup.zip /var/www -x "$Exclude"
Now, the problem is if Exclude variable contain multiple folders, it does not work, I mean this:
#!/bin/bash
Exclude="/ignore_dir_1/* /ignore_dir_2/*"
zip -r /var/backup.zip /var/www -x "$Exclude"
I even tried "${Exclude}" but no result.