Skip to content

subprocess.check_output fails when calling bash script with preceding newline #95575

Closed as not planned
@Juksefantomet

Description

@Juksefantomet

Bug report

python file test.py:

import subprocess
import re

rc = subprocess.check_output("/home/user/test.sh")
print(rc)

bash file test.sh:

<newline here>
#!bin/bash
echo "Hello World"

When executing the subprocess.check_output fails to reckognize the bash script as executable due to the newline.

SyntaxError: invalid syntax
Traceback (most recent call last):
  File "/home/user/test.py", line 5, in <module>
    rc = subprocess.check_output('/home/user/test.sh')
  File "/usr/lib64/python2.7/subprocess.py", line 216, in check_output
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
  File "/usr/lib64/python2.7/subprocess.py", line 394, in __init__
    errread, errwrite)
  File "/usr/lib64/python2.7/subprocess.py", line 1047, in _execute_child
    raise child_exception
OSError: [Errno 8] Exec format error

Your environment

Amazon Linux 2 - VM clean install
python 2.7.18
python 3.7.10

Is this expected behavior? or should there be any expected removing of preceding newlines?

Normal bash script execution works as intended even with newline in place.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions