Emacs Lisp: Call Shell Command

By Xah Lee. Date: . Last updated: .

Call Shell Command, Wait, Get Result

shell-command
Call a shell command, wait for it to finish.
shell-command-to-string
Call a shell command, wait for it to finish, get its output.
; call a shell command
(shell-command "ls")
; call a shell command and get its output
(shell-command-to-string "ls")

Call Shell, No Wait

start-process
(start-process NAME BUFFER PROGRAM &rest PROGRAM-ARGS)
Start a program in a subprocess. Return the process object for it. NAME is name for process. It is modified if necessary to make it unique. BUFFER is the buffer (or buffer name) to associate with the process.
Process output (both standard output and standard error streams) goes at end of BUFFER, unless you specify a filter function to handle the output. BUFFER may also be nil, meaning that this process is not associated with any buffer.
PROGRAM is the program file name. It is searched for in exec-path. If nil, just associate a pty with the buffer. Remaining arguments PROGRAM-ARGS are strings to give program as arguments.
If you want to separate standard output from standard error, use make-process or invoke the command through a shell and redirect one of them using the shell syntax.
The process runs in default-directory if that is local (as determined by unhandled-file-name-directory), or "~" otherwise. If you want to run a process in a remote directory use start-file-process.
start-process-shell-command
(start-process-shell-command NAME BUFFER COMMAND)
Use a single string for the external command and its args.
;; open files in Linux desktop
(mapc
 (lambda (x)
   (let ((process-connection-type nil))
     (start-process "" nil "xdg-open" x)) )
 filePathList)

Call PowerShell

Here's a example of how to call a PowerShell Core command.

;; on Microsoft Windows 10, call PowerShell Core to generate UUID
(shell-command "pwsh.exe -Command [guid]::NewGuid().toString()" t)
;; on macOS, call PowerShell Core to generate UUID
(shell-command "pwsh -Command '[guid]::NewGuid().toString()'" t)

Reference


Practical Elisp ⭐

Writing Command

Text Processing

Get User Input

File/Buffer

Writing Script