Search code examples
bashvisual-studio-code

How to run bash script within VS Code?


I want to run bash script when I hit F5 and see the results in the terminal like I can do with python scripts or whatever. I tried to do that with Bash Debug however it automatically goes to the debug mode and stops at the first step even if I do not put breakpoint. This is the launch configuration I use.

        {
            "type": "bashdb",
            "request": "launch",
            "name": "Run mysql test",
            "cwd": "${workspaceFolder}",
            "program": "/srv/gpf/dba/mysqlslap/run.sh",
            "args": []
        }

Solution

  • I don't know about running bash in a debug mode (doesn't seem like you need those features based on your example) but you can easily get your script to run as a Task or Build option.

    Place this at .vscode/tasks.json of your project dir

    {
        // See https://go.microsoft.com/fwlink/?LinkId=733558
        // for the documentation about the tasks.json format
        "version": "2.0.0",
        "tasks": [
            {
                "label": "Run as Bash Script",
                "type": "shell",
                "command": "/bin/bash ${file}",
                "problemMatcher": [],
                "group": {
                    "kind": "build",
                    "isDefault": true
                }
            }
        ]
    }
    

    You can place whatever you want in the "command" parameter.

    As you can see, it's of type "kind": "build" so I can hit ctrl+shift+B and this will execute in a terminal.

    Note, you can also use the command palette (F1 or ctrl+shift+P) and use Task: Run as Task too.

    Source: https://code.visualstudio.com/Docs/editor/tasks