VS Code无法识别无扩展名Bash脚本的Shebang,如何配置?
#!/bin/bash in Extensionless Bash Scripts I get exactly where you're coming from—VS Code plays nice with #!/bin/sh in extensionless files, switching straight to Shell Script mode, but it turns a blind eye to #!/bin/bash. Since you can't add a .sh extension (thanks to that supercomputing batch job naming rule jobname-batch) and need Bash specifically, here are your solutions:
Quick & Verified Fix: Mark the File as Executable
As pointed out in the comments, setting your script to executable makes VS Code automatically recognize the #!/bin/bash shebang and switch to the correct language mode. Just run this command in your terminal:
chmod +x your-jobname-batch-file
This method already solved your issue, so it's the first one to try.
Fallback: Manually Map Filenames to Bash Mode
If the executable trick doesn't work for some edge case, you can force VS Code to associate your extensionless batch files with Bash via settings:
- Open VS Code's settings (hit
Ctrl+,on Windows/Linux orCmd+,on macOS) - Search for
files.associations, then click "Edit in settings.json" - Add this line to your configuration (adjust the wildcard to match your filename pattern):
"files.associations": { "*-batch": "shellscript" }
For example, if all your batch files end with -batch, the *-batch wildcard will catch them all. This tells VS Code to treat any extensionless file matching that pattern as a Shell Script (Bash) file.
内容的提问来源于stack exchange,提问作者Carlos Garcia-Vaso

