Elisp: Get Command Line Arguments

By Xah Lee. Date: . Last updated: .

This page shows you how to get command line argument's values in a emacs lisp batch script.


You can run emacs lisp scripts from the OS's command line just like Perl, Python, Ruby, scripts. Like this:

emacs --script process_log.el

Sometimes you want to pass a argument. For example, like this:

emacs --script process_log.el ~/weblog.txt

In your script process_log.el, you want to know the file name passed to it. How to do that?


“argv” Variable Hold Arguments

Argument from the command line is stored in the built-in elisp variable argv. Its value is a list. Each element is a item from the command line.

Sample Script

Here's a sample test script:

(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〕.

Patreon me $5 patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

If you have a question, put $5 at patreon and message me.