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 Elisp 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.

(message "argv 0: %s" (elt argv 0)) ; %s is for string
(message "argv 1: %s" (elt argv 1))
(message "argv 2: %s" (elt argv 2))
(message "argv 3: %s" (elt argv 3))

Save and name the above script as test.el and run it like this:

emacs --script test.el uni 2 -tri

Here's the output:

$ emacs --script test.el uni 2 -tri
argv 0: "uni"
argv 1: "2"
argv 2: "-tri"
argv 3: nil

(info "(elisp) Command-Line Arguments")

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

Elisp, Writing Scritpts

  1. Run Elisp Script in Shell
  2. Get Command Line Args
  3. Read File
  4. Read File Content as List
  5. Write File
  6. Print, Output
  7. Format String
  8. Walk Dir
  9. Call Shell Command
  10. Get Script Name at Run Time

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs


Emacs Lisp