title: Bash parallization using & stored in a variable
body: <p>I'm writing a bash script and intend to loop over some files, allowing for parallel execution of the commands by executing the single commands in the background (&amp;). Furthermore I want to use a command line option, that enables parallelization/background execution. In a first case this has been working out using eval.</p>
  
  <pre><code>if [ ! -z $parallelization ]
  then
    PARALLEL='&amp;'
  else
    PARALLEL=''
  fi
  
  ...
  
  for file in list_of_files
  do
    # eval needed to turn $PARALLE into &amp;
    eval command $file $PARALLEL
  done
  </code></pre>
  
  <p>With a singel command that's been working nicely so far. The problem starts when I have to execute various commands within the loop and want to put all of them into the background. The following code is not working:</p>
  
  <pre><code>for file in list_of_files
  do
    eval {
    # piping is not working with command_1 and 2
    command_1 $file &gt; intermediateFile
    command_2 intermediateFile &gt; result
    rm intermediateFile;
    } $PARALLEL
  done
  </code></pre>
  
  <p>But this doesn't work at all:</p>
  
  <pre><code>./variant_calling.sh: line 20: syntax error near unexpected token `}'
  ./variant_calling.sh: line 20: `               } ${PARALLEL}'
  </code></pre>
  
  <p>Simply removing eval doesn't work either:</p>
  
  <pre><code>./variant_calling.sh: line 20: syntax error near unexpected token `${PARALLEL}'
  ./variant_calling.sh: line 20: `               } ${PARALLEL}'
  </code></pre>
  
  <p>What works is:</p>
  
  <pre><code>for file in list_of_files
  do
    {
    # piping is not working with command_1 and 2
    command_1 $file &gt; intermediateFile
    command_2 intermediateFile &gt; result
    rm intermediateFile;
    } &amp;
  done
  </code></pre>
  
  <p>But here the parallelization is always enabled. So is there a way to still use the variable $PARALLEL containing the &amp; if background execution is desired?</p>
  
  <p>Any help to solve this probleme will be highly appreciated!</p>
  
  <p>Thanks for any help!</p>