1

I am trying to run bash script.sh file in Python.
I tried running it through following code:

subprocess.call("script.sh", shell=True)

but it runs the script outside python and asks Windows application to run this file.

I want this script.sh to be run in Python command line.
I want Python to run this script.

12
  • How can Python run a shell script? Commented Apr 24, 2017 at 17:08
  • You are running on windows? This runs cmd.exe which runs your program. Set shell=False and it will run it directly... but either way, its got to call a program that understands sh syntax. That doesn't happen natively on windows so you need something like cygwin or gitbash. Alternately you can rewrite the script in python and run that. Commented Apr 24, 2017 at 17:13
  • Yes im on windows but im using python compiler pycharm and i have installed python for windows already.doesnt python itself supports bash? ^ Commented Apr 24, 2017 at 17:18
  • i want bash script outputs on pycharm commandline. thats it. Commented Apr 24, 2017 at 17:19
  • 1
    No, python itself knows nothing of bash. its a different syntax. Commented Apr 24, 2017 at 17:19

1 Answer 1

-1

Import the library OS and then use os.system:

import os

os.system('bash')
Sign up to request clarification or add additional context in comments.

2 Comments

This does works but it asks for windows application to run the bash file. while what i am trying to do is run is bash file within pycharm command line.
that's right (I didn't read the whole question), this run in linux (I've no idea of windows), in linux I added the vte widget and then run scripts in my program: self.terminal.feed_child("sh test2\n",len("sh test2\n"))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.