Assignments are shell syntax.
When you don't set shell=True
, there is no shell, so there's nothing available to parse and honor the assignment.
Just setting shell=True
would cause security bugs.
If your filename contains $(rm -rf ~)
, using ''.format()
to inject it is dangerous.
So generate an environment that has LC_ALL
added to it, with a value of C
:
sort_env = os.environ.copy()
sort_env['LC_ALL'] = 'C'
subprocess.check_call(['sort', '-k1', file, '-o', file], env=sort_env)
Or use shell=True
, but pass arguments out-of-band:
When a list is passed to shell=True
, the first element is treated as the script to interpret; the second as $0
in the context of that script; the third as $1
, etc. Thus:
subprocess.check_call(['LC_ALL=C sort -k1 "$1" -o "$2"', '_', file, file])