title: Bash command substitution executing before earlier code
body: <pre><code>ssh -i "path/to/my/key.key" ip.ip.ip.ip "mkdir a/a; sql 'query;' &gt; a/a/file.txt; if [ -f a/a/file.txt ]; then if [ $(wc -l a/a/file.txt) -ge 2 ]; then echo 'done'; fi; fi; exit"
  </code></pre>
  
  <p>After connecting via ssh, I'm basically making a directory, writing a sql query to a file. If that file exists, then I check if its word count is greater than 1. If word count greater than 1, I echo done, then exit.</p>
  
  <p>However, I get this error:</p>
  
  <pre><code>wc: a/a/file.txt open: No such file or directory
  </code></pre>
  
  <p>It seems as if the command substitution <code>$(wc -l a/a/file.txt)</code> is executing first (at least before the sql query), so the file hasn't been created yet. Is there some way to force this to execute in the order its written, or is there some better way to do this?</p>