ELisp: Get Command Line Arguments

By Xah Lee. Date: . Last updated: .

when you run emacs lisp script from terminal

emacs --script do.el arg1 arg2

[see ELisp: Run Emacs Lisp Script in Shell]

You can get the arguments in elisp from the variable argv

argv
A built-in variable. Its value is a list. Each element is a item from the command line.
;; 2023-08-03
;; a test emacs script

;; save this file as test.el
;; run in shell like this
;; emacs --script test.el 0 1 2

;; print arguments from command line
;; %s is for string
(message "argv 0: %s" (elt argv 0))
(message "argv 1: %s" (elt argv 1))
(message "argv 2: %s" (elt argv 2))
(message "argv 3: %s" (elt argv 3))

;; ~/web/xahlee_info/emacs/emacs $ emacs --script test.el 0 1 2
;; argv 0: 0
;; argv 1: 1
;; argv 2: 2
;; argv 3: nil
;; ~/web/xahlee_info/emacs/emacs $
emacs lisp script 2023-08-03
emacs lisp script 2023-08-03

Command-Line Arguments (ELISP Manual)

Thanks to Piotr Chamera [piotr_cham…@poczta.onet.pl], Swami Tota Ram Shankar [tota_…@india.com].