Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The bash documentation says in its <a href="http://www.gnu.org/software/bash/manual/bash.html#Pipelines" rel="noreferrer">section about pipelines</a>:</p> <blockquote> <p>Each command in a pipeline is executed in its own subshell [...]</p> </blockquote> <p>"In its own subshell" means that a new bash process is spawned, which then gets to execute the actual command. Each subshell starts successfully, even when it immediately determines that the command it is asked to execute doesn't exist.</p> <p>This explains why the entire pipe can be set up successfully even when one of the commands is nonsense. Bash does not check if each command can be run, it delegates that to the subshells. That also explains why, for example, the command <code>nonexisting-command | touch hello</code> will throw a "command not found" error, but the file <code>hello</code> will be created nonetheless.</p> <p>In the same section, it also says:</p> <blockquote> <p>The shell waits for all commands in the pipeline to terminate before returning a value.</p> </blockquote> <p>In <code>sleep 5 | nonexisting-command</code>, as A.H. pointed out, the <code>sleep 5</code> terminates after 5 seconds, not immediately, hence the shell will also wait 5&nbsp;seconds.</p> <p>I don't know why the implementation was done this way. In cases like yours, the behavior is surely not as one would expect.</p> <p>Anyway, one slightly ugly workaround is to use FIFOs:</p> <pre><code>mkfifo myfifo ./long-running-script.sh &gt; myfifo &amp; whoops-a-typo &lt; myfifo </code></pre> <p>Here, the <code>long-running-script.sh</code> is started and then the scripts fails immediately on the next line. Using mutiple FIFOs, this could be extended to pipes with more than two commands.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload