bashshell

Running Bash pipe commands in background with & ampersand


Consider:

time for i in `ls /tmp/chunk*`; do (cat $i | tr ' ' '\n' | sort | uniq > /tmp/line${i:10}) & ;done

Output:

bash: syntax error near unexpected token `;'

What’s the syntax error in the above command? I also tried using {} and ended the piped commands with ;. But the same error shows up...


Solution

  • You should put the & inside the (), if you want to run all the jobs in parallel in the background.

    time for i in `ls /tmp/chunk*`; do
      (cat $i | tr ' ' '\n' | sort | uniq > /tmp/line${i:10} &)
    done