Windows Bash and Visual Studio Code: How can I launch bash as a run task?
Asked Answered
H

3

10

how can I run the Windows Bash from Visual Studio code as a run task?

Here are some of my many attempts at tasks.json to do this.

{
    "version": "0.1.0",
    "command": "cmd",
    "isShellCommand": true,
    "args": [ "RunShellScript.bat" ],
    "showOutput": "always"
}

RunShellScript.bat has only this line: bash myShellScript.sh. Which if you just open cmd from start, and type that line, it will execute. It also runs perfectly if you just double click the file as well. However, when launched from VSCode, this output just hangs until I terminate it.

Attempt number 2:

{
    "version": "0.1.0",
    "command": "cmd",
    "isShellCommand": true,
    "args": [ "bash myShellScript.sh" ],
    "showOutput": "always"
}

This also hangs, just like above.

Attempt number 3:

{
    "version": "0.1.0",
    "command": "C:/Windows/System32/bash",
    "isShellCommand": false,
    "args": [ "myShellScript.sh" ],
    "showOutput": "always"
}

This gives me:

Failed to launch external program C:/Windows/System32/bash myShellScript.sh
spawn C:/Windows/System32/bash ENOENT

Ive also experimented with setting the "isShellCommand" variable to true and false in some of these cases, but to no avail.

Does anyone know how to do this?

Hart answered 16/5, 2016 at 6:42 Comment(0)
P
8

I too was looking to see how I could do the same thing.

andlrc - I've tried your two options and neither of them seem to do the trick. The issue seems to be in that bash is not recognised in whatever context Visual Studio Code runs it in. I've tried various approaches:

  • "command": "C:/Windows/System32/bash.exe"
  • "command": "bash"
  • "command": "bash.exe"

I'll keep trying and post back if I get something working.

Edit: Got it. Credit to this man - https://aigeec.com/using-windows-10-anniversary-bash-with-visual-studio-code/

In summary, what it boils down to is that I am on a 64 bit operating system which means bash.exe is located in C:\Windows\System32. Once i'd copied bash.exe into SysWOW64 as the blog post suggests, the default build action (Ctrl+Shift+B) of Visual Studio Code runs as you'd expect it to.

This seems a bit of a hacky workaround - it's probably worth raising with MSFT to see if there's something we can do about it.

Edit: Here's my tasks.json as requested:

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "0.1.0",
  "command": "bash",
  "isShellCommand": true,
  "args": [ "./gulp.sh" ],
  "showOutput": "always",
  "tasks": [{
    "taskName": "scripts",
    "isBuildCommand": true,
    "problemMatcher": "$gulp-tsc",
    "isWatching": true
  }]

}

gulp.sh contains only one command; 'gulp' :-) but you could essentially put whatever you want in there, I suppose you could have several .sh scripts to cater for the different tapes of tasks

In response to the use of Sysnative, unfortunately it doesn't seem to understand that and the build task does not run.

Particulate answered 16/5, 2016 at 12:59 Comment(5)
A less hacky solution would be to try to run C:\Windows\sysnative\bash.exe.Eddaeddana
Could you edit your post to include the tasks.json or launch.json you used as well?Hart
My so called 'solution' has, for whatever reason, stopped working... Investigating.Particulate
Jason: See comment from @RossRidge: You need to change your "command" to call "c:\\Windows\\sysnative\\bash.exe"Cornwall
This helped me a lot. I also added "--login" to the args so that .bashrc would be executed in order to make sure my environment was setup properly.Digamy
M
3

On my Windows 10 machine that has a freshly installed bash, this works fine for me:

{
    "version": "0.1.0",
    "command": "bash",
    "isShellCommand": true,
    "args": [ "shellscript.sh" ],
    "showOutput": "always"
}

The shebang version of shellscript.sh as command doesn't work for me.

You can also use bash -c "<command>" to run an arbitrary command:

{
    "version": "0.1.0",
    "command": "bash",
    "isShellCommand": true,
    "args": ["-c", "echo \"test\""],
    "showOutput": "always"
}

enter image description here

Mckibben answered 22/5, 2016 at 18:53 Comment(2)
32 or 64 bit? and what is in your shellscript.sh? I opened an issue with Microsoft regarding this and they were able to replicate the same issues I had - linkParticulate
64 bit, let's move the discussion to github.Mckibben
L
1

Try:

{
    "version": "0.1.0",
    "command": "bash",
    "isShellCommand": true,
    "args": [ "myShellScript.sh" ],
    "showOutput": "always"
}

Or you can add a shebang to your file:

#!/bin/bash

and then write the path to the script:

{
    "version": "0.1.0",
    "command": "/path/to/script.sh",
    "isShellCommand": true,
    "args": [],
    "showOutput": "always"
}
Lipfert answered 16/5, 2016 at 8:5 Comment(1)
My script does have the shebang up top, but I will try both of theseHart

© 2022 - 2024 — McMap. All rights reserved.