首页 > 解决方案 > 指定 tasks.json shell 命令的路径

问题描述

在 tasks.json 中,如何指定 shell 命令的路径?我试过:

{
  "label": "launch site local (ngserve)",
  "type": "shell",
  "group": "build",
  "command": "C:/Users/me/npm start",
  "problemMatcher": ["$tsc"]
}

退出并出现错误:

/usr/bin/bash: C:/Users/me/npm: No such file or directory 终端进程以退出代码终止:127

标签: visual-studio-codevscode-tasks

解决方案


我意识到命令字符串可以包含多个命令,所以我使用 && 链接它们并做了:

{
  "label": "launch site local (ngserve)",
  "type": "shell",
  "group": "build",
  "command": "cd C:/Users/me && npm start",
  "problemMatcher": ["$tsc"]
}

请注意,此解决方案是特定于默认 shell 的(powershell 不能很好地处理这个问题)——我在 windows 上使用 Git Bash。

您可以通过将选项参数添加到上述任务来更改特定命令的默认 shell。选项参数如下所示:

更改 git bash shell 位置的路径

  "options": {
    "shell": {
      "executable": "C:\\Windows\\System32\\cmd.exe",
      "args": ["/d", "/c"]
    }

推荐阅读