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.

2 Answers 2

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

2
  • Hmm... so \ would not be treated as part of the comment? Here(superuser.com/questions/641952/…) the OP says he cannot do this.
    – rivanov
    Commented Jun 16, 2015 at 21:18
  • @rivanov the comment has to be put before the newline escape for it to be evaluated. The error that you linked was because the comment was being evaluated after the newline, voiding the whole line as a comment
    – td512
    Commented Jun 16, 2015 at 21:31
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.)

2
  • That's insightful. So, done needs to be on its own line?
    – rivanov
    Commented Jun 16, 2015 at 21:21
  • done can be alone on a line, or separated by ';' from the previous command.
    – MaxP
    Commented Jun 16, 2015 at 21:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .