Function: shell-script-mode

shell-script-mode is a and interactive for sh-mode, defined in sh-script.el.gz.

Signature

(shell-script-mode)

Documentation

Major mode for editing shell scripts.

This mode works for many shells, since they all have roughly the same syntax, as far as commands, arguments, variables, pipes, comments etc. are concerned. Unless the file's magic number indicates the shell, your usual shell is assumed. Since filenames rarely give a clue, they are not further analyzed.

This mode adapts to the variations between shells (see sh-set-shell) by means of an inheritance based feature lookup (see sh-feature). This mechanism applies to all variables (including skeletons) that pertain to shell-specific features. Shell script files can use the sh-shell local variable to indicate the shell variant to be used for the file.

The default style of this mode is that of Rosenblatt's Korn shell book. The syntax of the statements varies with the shell being used. The following commands are available, based on the current shell's syntax:
C-c C-c (sh-case) case statement
C-c C-f (sh-for) for loop
C-c ( (sh-function) function definition
C-c TAB (sh-if) if statement
C-c C-l (sh-indexed-loop) indexed loop from 1 to n
C-c C-o (sh-while-getopts) while getopts loop
C-c C-r (sh-repeat) repeat loop
C-c C-s (sh-select) select loop
C-c C-u (sh-until) until loop
C-c C-w (sh-while) while loop

For sh and rc shells indentation commands are:
C-c ? (smie-config-show-indent) Show the rules controlling this line's indentation.
C-c < (smie-config-set-indent) Change the rules controlling this line's indentation.
C-c > (smie-config-guess) Try to tweak the indentation rules so the
buffer indents as it currently is indented.


DEL (backward-delete-char-untabify) Delete backward one position, even if it was a tab.
M-e (sh-end-of-command) Go to end of successive commands.
M-a (sh-beginning-of-command) Go to beginning of successive commands.
C-c : (sh-set-shell) Set this buffer's shell, and maybe its magic number.
C-M-x (sh-execute-region) Have optional header and region be executed in a subshell.

sh-electric-here-document-mode(var)/sh-electric-here-document-mode(fun) controls whether insertion of two unquoted < insert a here document. You can control this behavior by modifying sh-mode-hook.

If you generally program a shell different from your login shell you can set sh-shell-file accordingly. If your shell's file name doesn't correctly indicate what shell it is use sh-alias-alist to translate.

If your shell gives error messages with line numbers, you can use C-c C-x (executable-interpret) with your script for an edit-interpret-debug cycle.

Probably introduced at or before Emacs version 28.1.

Key Bindings

Aliases

shell-script-mode