Elisp: Call Shell Command

By Xah Lee. Date: . Last updated: .

Call Shell Command, Wait, Get Result

shell-command

(shell-command COMMAND &optional OUTPUT-BUFFER ERROR-BUFFER)

Call a shell command, wait for it to finish.

;; call a shell command

(cond
 ((eq system-type 'windows-nt)
  (shell-command "date"))
 ((eq system-type 'gnu/linux)
  (shell-command "date"))
 ((eq system-type 'darwin)
  (shell-command "date"))
 (t (shell-command "date")))
shell-command-to-string

(shell-command-to-string COMMAND)

Call a shell command, wait for it to finish, return its output as string.

; call a shell command and get its output
(shell-command-to-string "ls")

Call Shell Command with Text Selection as Input

shell-command-on-region

Call Shell Command, Do Not Wait (async)

async-shell-command

(async-shell-command COMMAND &optional OUTPUT-BUFFER ERROR-BUFFER)

similar to shell-command but don't wait for it to finish.

💡 TIP: this command relies on the shell async operator & to work. So is not guaranteed to work. If you truly want async, use start-process. 〔see Elisp: Start External Process

Examples: Command Wrapper Calling External Program

Emacs Lisp, Call Shell Command