If you want to use Bash features (arrays, process substitution, here strings, or a lot of other non-POSIX extensions and enhancements), you need to explicitly override the default shell:
subprocess.check_call(
'wc -l <(pwd)',
executable='/bin/bash', # the beef
shell=True)
or - somewhat more clumsily - run an explicit Bash instance:
subprocess.check_call(
['/bin/bash', '-c', 'wc -l <(pwd)'])
Notice how in the latter case we avoid separately specifying shell=True
, and pass in the script as a list of strings (where the third string is an arbitrarily complex and/or long script as the argument to bash -c
).
(Actually there is a length limit. If your command line is longer than the kernel constant ARG_MAX
you'll need to pass the script in a file or as standard input to the shell instead. On any modern system, we are talking megabytes of script, though.)
Running complex shell scripts (Bash or otherwise) from Python is dubious, anyway; you'll want to delegate absolutely as little as possible to a subprocess
and take it from there in native Python code.
As a belated aside, running wc
on a directory is weird. You also don't need pwd
at all;
wc -c .
Perhaps see also What exactly is current working directory?