-->

Why is “& ;” invalid syntax in bash?

2019-03-02 16:41发布

问题:

I am trying to run a for loop on the terminal where I want to send each iteration to background process so that all of them run simultaneously.

Following is the command running one by one

for i in *.sra; do fastq-dump --split-files $i ; done  # ";" only

I have highlighted the semicolon.

To run simultaneously this works

for i in *.sra; do fastq-dump --split-files $i & done  # "&" only

but this gives an error

for i in *.sra; do fastq-dump --split-files $i & ; done  # "& ;"

It would be nice if some one explains what is going on here. I know this should be written in a shell script way with proper indentation, but some times I only have this command to run.

Thanks

回答1:

& and ; both terminate the command that precedes them.

You can't write & ; any more than you could write ; ; or & &, because the language only allows a command to be terminated once (and doesn't permit a zero-word list as a command).


Thus: for i in *.src; do fastq-dump --split-files "$i" & done is perfectly correct as-is, and does not require an additional ;.