Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting bash variable into filename for zip command

Tags:

bash

zip

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
like image 314
Jared Henderson Avatar asked Mar 06 '12 18:03

Jared Henderson


People also ask

How do I zip a file in shell script?

Syntax : $zip –m filename.zip file.txt 4. -r Option: To zip a directory recursively, use the -r option with the zip command and it will recursively zips the files in a directory. This option helps you to zip all the files present in the specified directory.

How do I zip from command line?

The easiest way to zip a folder on Linux is to use the “zip” command with the “-r” option and specify the file of your archive as well as the folders to be added to your zip file. You can also specify multiple folders if you want to have multiple directories compressed in your zip file.

How do I get filenames without an extension in Unix?

If you want to retrieve the filename without extension, then you have to provide the file extension as SUFFIX with `basename` command. Here, the extension is “. txt”.


1 Answers

you can use ${VERSION_STRING} to clearly wrap your variable name

like image 105
cyber-monk Avatar answered Sep 21 '22 06:09

cyber-monk