When I open a shell script (e.g. emacs myscript.sh) and do C-c : (M-x sh-set-shell), and select sh as the shell, Emacs will insert a #!/usr/bin/sh shebang instead of #!/bin/sh. I want Emacs to insert #!/bin/sh instead of #!/usr/bin/sh. I realize that Emacs is probably behaving like this because /usr/bin comes before /bin in my shell's PATH. What are some ways to prioritize the /bin directory only when running sh-set-shell?

  • sh-set-shell allows you to just say /bin/sh, and it will use that.
    – rpluim
    Feb 18, 2021 at 10:49

1 Answer 1


Simply add an advice around sh-set-shell will do.

(defun sh-set-shell-tmp-path-advice (func &rest r)
  "Wrap `sh-set-shell' with temporarily modified
  (let ((exec-path (cons "/bin/" exec-path)))
    (apply func r)))

(advice-add 'sh-set-shell
            :around 'sh-set-shell-tmp-path-advice)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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