Reputation: 728
I'm trying to put the amount of lines in a gzipped file in a variable, later on I plan to use this stdout for another process using tee. Why won't the value of wc -l get put into the variable and how can I fix this?
[]$ gzip -dc BC102.fastq.gz | wc -l
4255588
[]$ gzip -dc BC102.fastq.gz | echo $(wc -l)
4255588
[]$ gzip -dc BC102.fastq.gz | reads=$(wc -l); echo $reads
0
The whole line is eventually supposed to look like
gzip -dc BC102.fastq.gz | tee >(reads=$(wc -l)) | cutadapt -ga...
I don't see how this is a duplicate from How to set a variable to the output from a command in Bash? since I was already applying the answer listed there to echo the value of wc -l directly, but it won't be inserted into the variable.
Upvotes: 2
Views: 1650
Reputation: 241701
If you set a variable in a subshell, it will have no effect on the parent. That is what happens with
gzip -dc BC102.fastq.gz | tee >(reads=$(wc -l)) | cutadapt -ga...
But nothing obvious stops you from continuing the pipe in the subshell:
reads=$(wc -l <(gzip -dc BC102.fastq.gz | tee >(cutadapt -ga... )))
Upvotes: 2
Reputation:
tee
writes to stdout plus to all files given as arguments. It does not write to two different pipes you could attach to.
Try this:
t=$(tempfile)
reads=$(gzip -dc BC102.fastq.gz | tee $t | wc -l)
Now you can continue in your script
cutadapt -ga $t
while reads
contains the number of lines.
Upvotes: 3