Emacs Environment – Append Values to Environment Variables from Within Emacs

emacsenvironment-variables

I am using Elisp. I would like to do the following in my init file:

; Retrieve the value of LD_LIBRARY_PATH
; Append '/path/to/some/lib/:'to it
; Save the new value of LD_LIBRARY_PATH

So far I know that I can use:

(getenv "LD_LIBRARY_PATH")

to retrieve the value, and:

(setenv "LD_LIBRARY_PATH" "foo")

to set a new value, but my knowledge of Elisp is extremely rudimentary so I don't know to proceed from this point.

Background:

This question is inspired by the fact that it is not be possible to source a shell script to manipulate this environment from within Emacs, so I would need to do this using Elisp.

Best Answer

In Elisp, string concatenation is done with concat:

(setenv "LD_LIBRARY_PATH"
  (let ((current (getenv "LD_LIBRARY_PATH"))
        (new "/path/to/some/lib"))
    (if current (concat new ":" current) new)))
Related Question