I've never saved any of my bash scripts with an extension and I've just recently pushed a bash script to Github and realized that it's not properly highlighting the correct syntax such as: comments, variables, loops, etc. Github has even tagged the script as a Javascript file and isn't reading the directive #!/bin/bash
. How can I resolve this issue and ensure my script is properly interpreted as a Bash script on Github?

- 111
- 4
2 Answers
Looking at the source code for GitHub's "linguist" [1] it needs to have a .sh
extension [2]
We use this library at GitHub to detect blob languages, highlight code, ignore binary files, suppress generated files in diffs and generate language breakdown graphs.
(Emphasis added by me)
[1] https://github.com/github/linguist
[2] https://github.com/github/linguist/blob/master/lib/linguist/languages.yml#L1190

- 5,396
- 3
- 32
- 51
Github looks for the shebang interpreter directive. This is the line at the top of the file which specifies what interpreter should be used to run the script.
An example of such a directive is:
#!/bin/bash
Github will highlight scripts containing such a directive, as well as those with a .sh extension (and a few others).
Your example script was identified correctly by github, but it was highlighted incorrectly. This suggests that you've somehow triggered a bug in their parser, linguist. This is something I would contact the linguist developers about.

- 244,070
- 43
- 506
- 972
-
Ive specified this at the top of my script, this is really all I should need for Github to determine that it's a script written in Bash. I still can't crack why it's not reading this properly. – Dford.py May 13 '13 at 23:38
-
Got an example? – Michael Hampton May 13 '13 at 23:41
-
Sure: https://github.com/Demet19/bash_scripts/blob/master/make_passwd – Dford.py May 13 '13 at 23:49
-
Hmm. Presuming you don't have something silly in that script like a BOM, I think you've discovered a bug in github's linguist. – Michael Hampton May 13 '13 at 23:51
-
I'm not even aware of what that is exactly, I've looked through to see if I've made any mistake and haven't been able to spot anything unusual. I've looked other bash scripts on Github and theirs works just fine with #!/bin/bash directive. Strange... – Dford.py May 13 '13 at 23:55
-
I looked at your script with `od` and it doesn't contain a [BOM](https://en.wikipedia.org/wiki/Byte_order_mark), so I think you can rule that out. – Michael Hampton May 13 '13 at 23:55