Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I specify the shell variant for a file in that file's Emacs local variables, when the file is not a standalone program?

Emacs allows me to specify that a shell include file should be opened in sh mode, by setting a local variable in the editor hints:

# Local variables:
# coding: utf-8
# mode: sh
# End:
# vim: fileencoding=utf-8 filetype=bash :

I can specify to Vim that the file's shell variant is specifically Bash, and it obeys the editor hint to present specific Bash syntax highlighting. But Emacs has sh mode which covers all shell variants.

Emacs will properly interpret such a file if it has a shebang line (e.g. #! /bin/bash). But some files are not stand-alone programs, so I do not want those to have a shebang line.

Emacs sh mode treats those files as Posix shell syntax, which they are not. sh mode has the concept of “variant” of shell syntax; but I'm unable to find out how to specify, in the file's editor hints, which variant to use for the file.

How can I set the editor hints in the file to tell Emacs that its syntax is Bash?

Update 2014-04-24: I have reported Emacs bug#17333 for this missing behaviour.

share|improve this question
bignose's bug report has been fixed. So in the next Emacs release (presumably 24.5), this really will be as easy as setting sh-shell as a file-local variable. Yay! Until then, my answer below really does work without adding an explicit shebang line (notwithstanding bignose's initial impression to the contrary). – Ben Liblit Jun 25 '14 at 15:35

Looking at the sh-mode code, it seems that someone may have intended to allow this by setting sh-shell or perhaps sh-shell-file as a file-local variable. Neither actually works, though. Please consider submitting a bug report to the sh-mode maintainer about this. It really seems like it should be as easy as adding sh-shell: bash to your file-local variables block, and that may have even been the original intent, but it just doesn’t work.

What does work is either of the following:

  1. Name your file something ending in .bash.

  2. Add eval: (sh-set-shell "bash") to your file-local variables block. By default Emacs will show a warning that this might be unsafe code, though that can be disabled.

share|improve this answer
I've submitted a bug report, thanks. – bignose Apr 24 '14 at 0:08
The sh-set-shell command does the wrong thing: it adds a shebang line, to a file that specifically should not have one. – bignose Apr 24 '14 at 0:08
sh-set-shell only adds a shebang line when called interactively, or with a non-nil third argument. When called as I suggest above, it does not add a shebang line. Try it and see for yourself. – Ben Liblit Apr 24 '14 at 15:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.