The default action for the SIGPIPE signal is to terminate the program. The Python interpreter changes it to SIG_IGN to be able to report broken pipe errors to the program as exceptions.
When you execute cat ... |head ... in a shell, cat has a default SIGPIPE handler, and the kernel simply terminates it on SIGPIPE.
When you execute cat using subprocess , it outputs the SIGPIPE handler from its parent (python interpreter), SIGPIPE is simply ignored, and cat handles the error itself by checking the errno variable and print error message. A.
To avoid error messages from cat , you can use the preexec_fn argument for subprocess.call:
from signal import signal, SIGPIPE, SIG_DFL subprocess.call( 'cat /dev/zero | head -c 10 | base64', shell = True, preexec_fn = lambda: signal(SIGPIPE, SIG_DFL) )
subdir
source share