Passing gunzipped filename to a variable

Hi ,
Am trying to gunzip a file and pass the gunzipped file name to a variable , but its not taking up the value. Am trying to execute the command f=`gunzip <filename>`;echo $f. Here the file is getting gunzipped but the file name is not assigned to the variable. Any help on this will be useful. Am trying to rename the gunzipped file to a specific format .
Thanks.

gunzip will rename the file for you.

If you need to control the filename of the uncompressed file, you can have gunzip write to a redirected standard output.

You might also be interested in the uncompressed filename displayed by -l . But, I don't know since your goal is a mystery.

If you need further help, why don't you show us the entire script, and explain what it is you are trying to accomplish.

Regards,
Alister

gunzip does not write anything to stdout that could be assign to the variable f. If you just want to assign the filename without the .gz suffix to f, write:

f=`basename filename .gz`

Another approach might be:

f=$(gzip -l filename | awk 'NR==2{print $NF}')
gunzip filename

Hi,
Thanks for the reply. This is the part of the code gunzip /tmp/$b where $b is having the zipped file name. I just want the file name to be gunzipped and the filename should be renamed to other format. So retrieving only the zipped filename will be useful

If you have the filename in $b , then what's the problem?

Not knowing more about the filename and what guarantees are in place regarding its suffix, nor knowing what you are trying to accomplish, I can't confidently suggest how to proceed.

If the suffix is recognized by gunzip, it will automatically strip the suffix when it decompresses the file. If the suffix is not recognized by gunzip, it will not work on the file. If you attempt to modify the filename in your shell variable independently of the renaming that gunzip may or may not perform, disagreement could ensue if error conditions aren't checked and handled diligently.

If you simply need the name of the decompressed file so that you can work with it, you may be better off handling all file renaming yourself and explicitly instructing gunzip what to name your file:

gunzip -c "$b" > newname

...where newname is something under your control.

If this isn't of any help to you, then, please, for the second time, just share your code. Your textual explanations so far have been inadequate. I'm not sure anyone understands the problem or the goal.

Regards,
Alister