Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Curly brace expansion fails on bash, in Linux, when called from Python

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading