zcat can assign as a variable in bash

542 Views Asked by At

I will like to unzip several files and merge it as ONE variable in bash.

For example: There are several text files.

one=text1.gz
two=text2.gz
three=text3.gz

I would like to merge them to become a variable

variable=(zcat $one $two $three)

So

$variable=($one)+($two)+($three)

How can I achieve this?

1

There are 1 best solutions below

3
On

You bash syntax isn't correct.

To assign a variable the result of the command, you need to use $(...):

variable=$(zcat "$one" "$two" "$three")

Depending on what you want to do with the content of these files, this might not be the best way forward.

You might want concatenate the files into another one using redirection:

zcat "$one" "$two" "$three" > combined_file

then work with the combine_file.

Or directly process command further using pipe |:

zcat "$one" "$two" "$three" | my_command

where my_command could be sed, awk, etc...