3

I have a line of bash:

SAMPLES=$(for f in `find $IN -iname *fastq.gz `; do basename $f | cut -c 1-4; done | sort | uniq)

which I am attempting to break up into multiple line for the purpose of commenting each of them. I'd like something like the following, with comments on each line or after the line:

SAMPLES=
#comment
$(for f in `find $IN -iname *fastq.gz `; \ 
#comment
do basename $f |
#comment
cut -c 1-4; done | 
#comment
sort |
#comment
uniq)

I've seen both this, and this, but they don't have the $() evaluation, or the for loop, which is throwing me off. Any input appreciated.

rivanov
  • 225
  • 2
  • 8

2 Answers2

3

You could use quite the syntax you want, but for the first line. If you write

SAMPLE=

Then variable SAMPLE is set to the empty string. But if you write

SAMPLE=$(

Then the interpreter looks for the closing parenthesis to end the statement. That is, you can write:

SAMPLES=$(
#comment
for f in $(find . -name *fastq.gz) ;
#comment
do
# comment
basename $f |
#comment
cut -c 1-4
done |
#comment
sort |
uniq)

(BTW, you can nest $() to avoid the older backquote syntax.)

MaxP
  • 221
1

You need to do this:

SAMPLES=$(for f in `find $IN -iname *fastq.gz `; #comment \
do basename $f | #comment \
cut -c 1-4; done |  #comment \
sort | #comment \
uniq)

This works because a comment ends at the newline \ and parses the command at the beginning of the next line

td512
  • 5,170