infoclogged
infoclogged

Reputation: 3997

running a simple ls command with subprocess and git bash

I am trying to call a git bash shell command from inside Windows operating system. Following is my code snippet. Can anyone please tell me, what I am doing wrong?

    git_bash_path = WindowsPath(r"c:\Users\abcd\AppData\Local\Programs\Git\git-bash.exe")
    command = "ls"
    subprocess.call([str(git_bash_path), command])

After running, the python script, it opens a bash window with the title in the window - /usr/bin/bash --login -i ls and in the git bash window the error is bash:/usr/bin/ls: cannot execute binary file.

I also tried -

    subprocess.check_output(command, shell=True, executable=str(git_bash_path)) 

but the error is the same.

Upvotes: 1

Views: 2424

Answers (2)

stapmoshun
stapmoshun

Reputation: 134

What worked for me, in case %PATH% does not have bash.exe, do

command = fr'"%ProgramFiles%\Git\bin\bash.exe" -c "echo hello"'
r = subprocess.Popen(command, shell=True)

This assumes that Git Bash is installed in default Program Files location.

Upvotes: 0

VonC
VonC

Reputation: 1323115

It should use the -c parameter:

bashCommand = "ls"
output = subprocess.check_output(['bash','-c', bashCommand])

Git for Windows comes with a bash.exe. It is is in the %PATH%, that should be enough.

Upvotes: 1

Related Questions