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

两盒软妹~` 提交于 2019-11-29 02:18:02

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.

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"
}

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"
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!