Search code examples
bashvisual-studio-codeshebang

VS Code does not recognize shebang for bash


According to this post, VS Code is supposed to recognize shebangs in files without extensions, and associate those files with the Shell Script language mode. In that post, they say it works for the shebang #!/bin/sh. However, this doesn't seem to work for shebang #!/bin/bash. I need to use Bash for my project, otherwise I would just switch to the Shell Command Language (sh). Also, I cannot add the .sh extension to the end of the filename, because I'm submitting a batch job to a supercomputer, and the filename needs to be named using the following pattern: jobname-batch. Is there a way to configure VS Code to have the same behavior for extension-less Bash scripts than for extension-less Shell Command Language scripts?

Edit:

@Julia suggested in the comments to make the file executable, and that got it working. :) Thanks!


Solution

  • The "set the executable bit" trick works, but I'm not sure where that information is stored or if it's persisted across reboots. I just remembered a better solution: file associations!

    Just add this to project/.vscode/vscode.json (the key should be the name of the file):

    {
      "files.associations": {
        "jobname-batch": "bash"
      }
    }