Difference Between Emacs's “(getenv PATH)” and “exec-path”

Perm url with updates: http://xahlee.org/emacs/emacs_env_var_paths.html

Difference Between Emacs's “(getenv PATH)” and “exec-path”

Xah Lee, 2009-08-04, 2010-09-23

This page explains the mechanisms of setting environment variables in emacs, especially if you have problems in Windows emacs of getting aspell or other unix utils to run.

  • When you start emacs from a shell, emacs inherits shell's environment variables. (true on Windows, Mac, Linux)
  • On Windows, when you start emacs from GUI, emacs also inherit environment variables, from the Registry.
  • On Mac OS X, when you start emacs from GUI, emacs does not inherit environment variables from your shell, but does inherit the system-wide environment variables from 〔~/.MacOSX/environment.plist〕.
  • On Mac OS X, you can start GUI emacs from shell, like this: 「nohup /Applications/Emacs.app/Contents/MacOS/Emacs &」.

If you are not familiar with env var on Windows or Registry, see:

Setting Environment Variable Within Emacs

You can also set environment variables within emacs without actually setting them in the OS. To do so, use this sample code:

; show env var named path
(getenv "PATH")

; example of setting env var named “path”
; by appending a new path to existing path
(setenv "PATH"
  (concat
   "C:/cygwin/usr/local/bin" ";"
   "C:/cygwin/usr/bin" ";"
   "C:/cygwin/bin" ";"
   (getenv "PATH")
  )
)

In some situations, it's better to set some env var inside emacs for emacs only. This way, you are free to have a different env var value in your cmd.exe or cygwin shell, independent of emacs.

Emacs's “exec-path”

Emacs has a variable named “exec-path”. Its value is a list of dir paths. Emacs uses “exec-path” to find executable binary programs. For example, when spell checking, emacs will try to find ispell or aspell in exec-path. When you press 【Z】 to compress file in dired, emacs will try to find gzip or gunzip in exec-path. When you type 【Alt+x diff】 or 【Alt+x grep】or 【Alt+x shell】, emacs will try to find the program in exec-path too.

If emacs complains that it cannot find ispell, aspell, ftp, gzip, etc, the problem is probably with your “exec-path”.

By default, emacs copies the value of 「(getenv "PATH")」 to “exec-path”. So, their values should be identical.

Difference between “exec-path” and “PATH”

The value of “PATH” is used by emacs when you are running a shell in emacs, similar to when you are using a shell in a terminal.

The “exec-path” is used by emacs itself to find programs it needs for its features, such as spell checking, file compression, compiling, grep, diff, etc.

If you did set the PATH env var within emacs, you probably also want to adjust your “exec-path”. Here's a example of setting exec-path:

(when (string-equal system-type "windows-nt")
  (setq exec-path
'(
"C:/Program Files (x86)/Emacs/emacs/bin/"
"C:/Program Files (x86)/Emacs/EmacsW32/gnuwin32/bin/"
"C:/Windows/system32/"
"C:/Windows/"
"C:/Windows/System32/Wbem/"
"C:/Windows/system32/WindowsPowerShell/v1.0/"
)
 ))

The value of 「(getenv "PATH")」 and “exec-path” do not need to be the same.


As of today (2009-08-04), my emacs has this setup:

(when (string-equal system-type "windows-nt")
  (progn
    ;; am using cygwin
    (setenv "PATH"
            (concat
             "/usr/local/bin" ":"
             "/usr/bin" ":"
             "/bin" ":"
             "/usr/X11R6/bin" ":"
             "/cygdrive/c/Windows/Program Files (x86)/PHP/" ":"

             "/cygdrive/c/Windows/system32" ":"
             "/cygdrive/c/Windows" ":"
             "/cygdrive/c/Windows/System32/Wbem" ":"
             "/cygdrive/c/Windows/system32/WindowsPowerShell/v1.0"
             ) )
    (setq exec-path
          '(
            "C:/cygwin/bin/"
            "C:/Program Files (x86)/Emacs/emacs/bin/"
            "C:/Program Files (x86)/PHP/"
            "C:/Windows/system32/"
            "C:/Windows/"
            "C:/Windows/System32/Wbem/"
            "C:/Windows/system32/WindowsPowerShell/v1.0/" )
          ) ) )

Note: the above example works for me in Windows Vista, but is not necessarily ideal.

For example, the path syntax used are different. Some uses “/” while others uses “\”, and some contains the drive name while others doesn't. Some with lower case drive letter, while other don't. Emacs has a wrapper of path conventions to OS, cygwin has its own, and also has some drive mapping mechanism. It is not clear to me how which path convention is best used in elisp to set PATH or exec-path.

See also:

Was this page useful? If so, please do donate $3, thank you donors!

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs