'When reading a file in emacs, how can I set the "shell" for Shell-script mode?

I have some files that are not shell scripts per se, but designed to be sourced by shell scripts. Therefore, the file name don't match any pattern in auto-mode-alist (nor am I interested in trying to update for each file).

When I edit one of my bash scripts (e.g., foo.bash), emacs properly deduces the shell and the mode line shows Shell-script[bash]. Whether this is based on filename or shebang line is immaterial, I think. I'd like to force this behavior for my "non-script" script files.

What I came up with was this:

# -*- mode: Shell-script; eval (sh_set_shell "bash"); -*-

But that gives a warning about the eval (rightly so). How else can I set the dialect for Shell-script mode?



Solution 1:[1]

You can use a file local variables block and set sh-shell in addition to the major mode:

# Local Variables:
# mode: shell-script
# sh-shell: bash
# End:

If you dislike this big (better readable :) ) block, a one liner will do niceley (per @jwm comment below):

# -*- mode: Shell-script; sh_shell: "bash"; -*-

Nota Bene: According to the documentation, using the file-local variable sh-shell is equivalent to calling sh-set-shell, and the intended way to set this up:

Shell script files can cause this function be called automatically when the file is visited by having a `sh-shell' file-local variable whose value is the shell name (don't quote it).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1