简体   繁体   中英

getting bash variable into filename for zip command

In a bash script, how do I use a variable to create a specifically named zipped file? For example, I would like to do something like:

VERSION_STRING='1.7.3'
zip -r foo.$VERSION_STRING foo

Where I ideally end up with a file called foo.1.7.3.zip

It seems like I'm having 2 problems:

  1. the zip command is treating $VERSION_STRING like it's null or empty
  2. the . after foo also seems to be mucking it up

您可以使用${VERSION_STRING}来清楚地包装变量名称

The following works fine here using bash 4.1.5:

#!/bin/bash

VERSION_STRING='1.7.3'
echo zip -r foo foo.$VERSION_STRING.zip

I've added the echo to see the actual command rather than run it. The script prints out

zip -r foo foo.1.7.3.zip

I think you have your zip inputs backwards. For me the following command:

zip -r foo.${VERSION_STRING}.zip foo

creates a valid zip file. Your command does not (at least with my version of zip).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM