My code is:


for seq in (fold -w3 $dnafile | uniq); do
              fold -w3 $dnafile | grep $seq | wc -l
              echo $seq
              echo '(new line)'

I receive the error:

syntax error near unexpected token `('
  • (1) Man pages are hard to read, but they are the best way to learn this stuff. (2) You should use quotes for "$dnafile" and "$seq". – Scott Oct 6 '17 at 19:41

It looks like you just want to count the number of unique lines resulting from fold -w3 $dnafile. This can be done without a loop:

fold -w3 "$dnafile" | sort | uniq -c

The sort would sort the lines of the output from fold while uniq -c would count the number of times each line occurs consecutively.

The output will be one line for each unique line in the fold result prefixed with a number.

The syntax error comes from the fact that a command substitution (i.e. a construct that is replaced by the output of a command) needs to look like $( ... ). I'm uncertain that inserting the missing $ will actually make your code work though, as the shell will still split the resulting text on newlines and spaces, but since we don't know what the input data looks like it's difficult to speculate.

Having a sub-shell, (...), in a for loop at that position is not allowed.


You lost the $ by (fold -w3 $dnafile | uniq). Try this: $(fold -w3 $dnafile | uniq).

$() is the syntax for command substitution, while () introduces a subshell.

  • It should also be quoted unless you want to split and glob the result. Surprisingly, this is even true in zsh. – Kevin Oct 6 '17 at 21:10
  • 1
    @Kevin, zsh does the split and not the glob. Here, the OP wants to split, otherwise there wouldn't be any point having a for loop. – Stéphane Chazelas Oct 6 '17 at 21:18
  • Note that in the fish shell, command substitution is with (...) instead of $(...) or `...` (in fish the syntax of the for loop is different though). And it also splits (though on newline only) and not globs. All shells split on cmdsubst as that's often what you want to do. – Stéphane Chazelas Oct 6 '17 at 21:20
  • OP may still want IFS="\n". – Kevin Oct 6 '17 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy