Consider this curly brace expansion in bash:
for i in {1..10}; do
echo $i;
done;
I call this script from the shell (on macOS or Linux) and the curly brace does expand:
$ ./test.sh
1
2
3
4
5
6
7
8
9
10
I want to call this script from Python, for example:
import subprocess
print(subprocess.check_output("./test.sh", shell=True))
On macOS, this Python call expands the curly brace and I see this output:
b'1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n'
On Linux, this Python call fails to expand the curly brace and I see this output:
b'{1..10}\n'
Why does curly brace expansion work on the interactive shell (macOS or Linux) and when called from Python on macOS, but fails when called from Python on Linux?
>Solution :
{1..10} is a bash feature, it is not defined in POSIX sh.
It seems that subprocess.check_output("./test.sh", shell=True) invokes bash (or another shell which supports this feature) in the first example (macOS) and sh in your second example (Linux).
See:
- https://www.shellcheck.net/wiki/SC3009
- Actual meaning of 'shell=True' in subprocess
- Does the `shell` in `shell=True` in subprocess mean `bash`?