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
  • 195
  • 1
  • 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.)

John Kugelman
  • 1,751
  • 15
  • 20
MaxP
  • 211
  • 1
  • 4
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,031
  • 2
  • 18
  • 41
  • Hmm... so \ would not be treated as part of the comment? Here(http://superuser.com/questions/641952/how-can-i-add-a-comment-for-each-flag-on-separate-lines-in-a-bash-script) the OP says he cannot do this. – rivanov Jun 16 '15 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 Jun 16 '15 at 21:31