Title: Unexpected behavior: zip -r in bash
Tags: bash;zip
Question: I have this script:
#!/bin/bash
date="20180313"
source_path="/Users/<user_name&gt;/Source/Git/WSI"
installer_path="../FE_Installer/cdk_installer/*"
echo "archive=$source_path/wsi_installers_$date.zip"
echo "path=$installer_path"
# Create the installer archive
zip -r "$source_path/wsi_installers_$date.zip" "$installer_path"
If the archive does not exist, it fails with:
zip warning: name not matched: ../FE_Installer/cdk_installer/*
zip error: Nothing to do! (try: zip -r /Users/<user_name&gt;/Source/Git/WSI/wsi_installers_20180313.zip . -i ../FE_Installer/cdk_installer/*)
Though the echo statements indicate a correct path:
archive=/Users/<user_name&gt;/Source/Git/WSI/wsi_installers_20180313.zip
path=../FE_Installer/cdk_installer/*
I tried the recommended command (using the -i option) but it fails with:
zip warning: zip file empty
If I use the output from the echo statements and run the zip command manually:
zip -r /Users/<user_name&gt;/Source/Git/WSI/wsi_installers_20180313.zip ../FE_Installer/cdk_installer/*
It works:
adding: ../FE_Installer/cdk_installer/ADP_Installers.hta (deflated 71%)
adding: ../FE_Installer/cdk_installer/StartHta.exe (deflated 71%)
adding: ../FE_Installer/cdk_installer/WSPCP_TDI/ (stored 0%)
adding: ../FE_Installer/cdk_installer/common/styles/plugins.css (deflated 85%)
adding: ../FE_Installer/cdk_installer/common/styles/table.css (deflated 80%)
adding: ../FE_Installer/cdk_installer/updatecenter/ (stored 0%)
Then when I run the script, it also works, with the output showing it is updating the archive:
updating: ../FE_Installer/cdk_installer/ADP_Installers.hta (deflated 71%)
updating: ../FE_Installer/cdk_installer/StartHta.exe (deflated 71%)
updating: ../FE_Installer/cdk_installer/WSPCP_TDI/ (stored 0%)
updating: ../FE_Installer/cdk_installer/common/styles/plugins.css (deflated 85%)
updating: ../FE_Installer/cdk_installer/common/styles/table.css (deflated 80%)
updating: ../FE_Installer/cdk_installer/updatecenter/ (stored 0%)
What am I missing?
Comment: Add `set -x` to your script.  That'll show what it thinks is happening.

Comment: I bet the `*` in `installer_path` isn't getting expanded.

Comment: + zip -r /Users/barnesr/Source/Git/WSI/wsi_installers_20180313.zip '../FE_Installer/cdk_installer/*'
zip warning: name not matched: ../FE_Installer/cdk_installer/*
zip error: Nothing to do! (try: zip -r /Users/barnesr/Source/Git/WSI/wsi_installers_20180313.zip . -i ../FE_Installer/cdk_installer/*)
+ exit

Comment: I suspected the '*' was a factor -- but why would it work when the ZIP exists?

Comment: Removing the '*' was the solution.  Thanks.  Add it as an answer and I'll mark it.

Here is the accepted answer: I bet the * in installer_path isn't getting expanded.  Take the * out of installer_path and it'll work.
Comment for this answer: @Walf that's mentioned in a comment I made to the question, before making an official answer.  I'll add it  to the answer for clarity.
Comment for this answer: This doesn't explain why, it works, which is because the `*` was quoted, so `zip` was looking for literal file/dir named `*`. You can put asterisks immediately after quoted parameters to retain shell globbing.


{'source': 'StackExchangeDataset', 'subset': 'unix.stackexchange.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2018:00:03/14/18', 'FavoriteCount': 0.0, 'PostTypeId': 1, 'Tags': '<bash><zip>', 'Title': 'Unexpected behavior: zip -r in bash', 'ViewCount': 5481, 'id': 430198, 'user_AboutMe': "<p>Retired computer engineer (DevOps 1985-2019).  Primarily Windows, some Linux, lot's of database and .NET.  ETL: Talend, DataStage; Database: MS SQL Server, Oracle; Windows OS: WSH (Jscript/VBscript), C#.NET; General: Bash, Javascript, PHP</p>\n", 'user_AccountId': 416110.0, 'user_DisplayName': 'AdvApp', 'user_Reputation': 121.0, 'user_Views': 5.0, 'user_id': 280271.0}}