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

If you have a question, put $5 at patreon and message me on xah discord.
Or support me by Buy Xah Emacs Tutorial

Emacs Tutorial

Emacs Init

Emacs Keys

ELisp

ELisp Examples

ELisp Write Major Mode


ELisp

Basics

Basic Functions

Writing Command

Writing Script

Lisp Data Structure

Lisp Symbol

Elisp Misc