Elisp: Run Elisp Script in Shell

By Xah Lee. Date: . Last updated: .

You can run emacs lisp script in shell (terminal), using the --script option. For example:

emacs --script process_log.el

Here's a table of most useful options for running emacs lisp as a script.

Option Full SyntaxOption Short SyntaxMeaning
--no-init-file-qDo not load your init files {~/.emacs, ~/.emacs.el, ~/.emacs.d/init.el} nor site-wide default.el.
--no-site-fileDo not load the site-wide site-start.el.
--batchDo not launch emacs as a editor. Use it together with --load to specify a lisp file. This implies --no-init-file but not --no-site-file.
--load="path"-l pathExecute the elisp file at path.
--script pathRun emacs like --batch with --load set to path.

For a complete list, see:

What's site-start.el?

The site-start.el is a init file for site-wide running of emacs.

It pretty much means a init file for all users of this emacs installation. It may be added by a sys admin, or it may be part of a particular emacs distribution (For example, Carbon Emacs, Aquamacs Emacs, …).

You can usually find this file in the directory where emacs is installed, if it exists. Typically in a directory named “site-lisp”. For example, /home/xah/apps/emacs-25.1/site-lisp/. But the file can be anywhere in emacs load-path.

Disable it if you want a pure GNU Emacs experience (without loading any packages added by third party)

(info "(elisp) Init File")

(info "(elisp) Library Search")

Prepare Your Emacs Lisp Script to Run in Batch Mode

When you write a elisp script to run in batch, make sure your elisp file is:

  1. self-contained; Doesn't depend on anything from your emacs init file.
  2. Explicitly load all libraries it needs (using require or load).
  3. Has necessary load path set in the script (For example, (add-to-list 'load-path lib_path)) if it needs libs that's not part of standard GNU emacs install, just like you would with a Python or Ruby script.

If you've done a clean job in your elisp script, then, all you need to use is:

emacs --script elisp_file_path

If your elisp program requires functions that you've defined in your emacs init file, then you should explicitly load it in your script by (load emacs init file path), or, you can add the option to load it, like this: --user=jane. (best to actually pull out the function you need)

How to start emacs on a Mac from command line?

If you are on a Mac with Carbon Emacs or Aquamacs, call it from the command line like this:

/Applications/Emacs.app/Contents/MacOS/Emacs --script=process_log.el
Like my tutorial? Put $5 at patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

Ask me question on patreon