Skip to main content
added 338 characters in body
Source Link
pLumo
  • 23.2k
  • 2
  • 43
  • 70

If you write ...

Exclude="/ignore_dir_1/* /ignore_dir_2/*"
zip -r /var/backup.zip /var/www -x "$Exclude"

, zip receives $Exclude as a single argument, and considers the space between the files as part of the path.


To pass multiple arguments to your command, you need to use an array.

Exclude=("/ignore_dir_1/*" "/ignore_dir_2/*")
zip -r /var/backup.zip /var/www -x "${Exclude[@]}"

This makes sure that the items are individually expanded and passed as arguments to your command.

To pass multiple arguments to your command, use an array.

Exclude=("/ignore_dir_1/*" "/ignore_dir_2/*")
zip -r /var/backup.zip /var/www -x "${Exclude[@]}"

If you write ...

Exclude="/ignore_dir_1/* /ignore_dir_2/*"
zip -r /var/backup.zip /var/www -x "$Exclude"

, zip receives $Exclude as a single argument, and considers the space between the files as part of the path.


To pass multiple arguments to your command, you need to use an array.

Exclude=("/ignore_dir_1/*" "/ignore_dir_2/*")
zip -r /var/backup.zip /var/www -x "${Exclude[@]}"

This makes sure that the items are individually expanded and passed as arguments to your command.

Post Undeleted by pLumo
Post Deleted by pLumo
added 338 characters in body
Source Link
pLumo
  • 23.2k
  • 2
  • 43
  • 70

To pass multiple arguments to your command, use an array.

Then you can make use of shell parameter expansion and substition:

Exclude=("/ignore_dir_1/*" "/ignore_dir_2/*")
zip -r /var/backup.zip /var/www "${Exclude[@]/#/-x "${Exclude[@]}"
  • Note, that each array item is quoted individually.
  • "${arr[@]}" will expand the array to the individual items
  • "${var/#/-x }" will add -x in front of the variable (# matches the beginning of a string)
  • The combination -> "${arr[@]/#/-x }" will expand the array and add -x in front of each array item.

To pass multiple arguments to your command, use an array.

Then you can make use of shell parameter expansion and substition:

Exclude=("/ignore_dir_1/*" "/ignore_dir_2/*")
zip -r /var/backup.zip /var/www "${Exclude[@]/#/-x }"
  • Note, that each array item is quoted individually.
  • "${arr[@]}" will expand the array to the individual items
  • "${var/#/-x }" will add -x in front of the variable (# matches the beginning of a string)
  • The combination -> "${arr[@]/#/-x }" will expand the array and add -x in front of each array item.

To pass multiple arguments to your command, use an array.

Exclude=("/ignore_dir_1/*" "/ignore_dir_2/*")
zip -r /var/backup.zip /var/www -x "${Exclude[@]}"
Source Link
pLumo
  • 23.2k
  • 2
  • 43
  • 70

To pass multiple arguments to your command, use an array.

Then you can make use of shell parameter expansion and substition:

Exclude=("/ignore_dir_1/*" "/ignore_dir_2/*")
zip -r /var/backup.zip /var/www "${Exclude[@]/#/-x }"
  • Note, that each array item is quoted individually.
  • "${arr[@]}" will expand the array to the individual items
  • "${var/#/-x }" will add -x in front of the variable (# matches the beginning of a string)
  • The combination -> "${arr[@]/#/-x }" will expand the array and add -x in front of each array item.