GNU Emacs Manual - Single Shell

Go to the first, previous, next, last section, table of contents.

### Single Shell Commands

`M-!` (`shell-command`) reads a line of text using the minibuffer and executes it as a shell command in a subshell made just for that command. Standard input for the command comes from the null device. If the shell command produces any output, the output goes into an Emacs buffer named ``*Shell Command Output*'`, which is displayed in another window but not selected. A numeric argument, as in `M-1 M-!`, directs this command to insert any output into the current buffer. In that case, point is left before the output and the mark is set after the output.

If the shell command line ends in ``&'`, it runs asynchronously. For a synchronous shell command, `shell-command` returns the command's exit status (0 means success), when it is called from a Lisp program.

`M-|` (`shell-command-on-region`) is like `M-!` but passes the contents of the region as the standard input to the shell command, instead of no input. If a numeric argument is used, meaning insert the output in the current buffer, then the old region is deleted first and the output replaces it as the contents of the region. It returns the command's exit status when it is called from a Lisp program.

Both `M-!` and `M-|` use `shell-file-name` to specify the shell to use. This variable is initialized based on your `SHELL` environment variable when Emacs is started. If the file name does not specify a directory, the directories in the list `exec-path` are searched; this list is initialized based on the environment variable `PATH` when Emacs is started. Your ``.emacs'` file can override either or both of these default initializations.

Both `M-!` and `M-|` wait for the shell command to complete. To stop waiting, type `C-g` to quit; that terminates the shell command with the signal `SIGINT`---the same signal that `C-c` normally generates in the shell. Emacs waits until the command actually terminates. If the shell command doesn't stop (because it ignores the `SIGINT` signal), type `C-g` again; this sends the command a `SIGKILL` signal which is impossible to ignore.

To specify a coding system for `M-!` or `M-|`, use the command `C-x `RET` c` immediately beforehand. See section Specifying a Coding System.

Error output from the command is normally intermixed with the regular output. If you set the variable `shell-command-default-error-buffer` to a string, which is a buffer name, error output is inserted before point in the buffer of that name.

Go to the first, previous, next, last section, table of contents.