Why does `timeout` not work with pipes?

Viewed 8239

The following command line call of timeout (which makes no sense, just for testing reason) does not work as expected. It waits 10 seconds and does not stop the command from working after 3 seconds. Why ?

timeout 3 ls | sleep 10
4 Answers

It is enough to set the timeout on the last command of the pipeline:

# Exits after 3 seconds with code 124
ls | timeout 3 sleep 10

# Exits after 1 second with code 0
ls | timeout 3 sleep 1
Related