My Emacs version is:

GNU Emacs 27.2 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.24, cairo version 1.16.0)

I have modified my eshell prompt as follows:

(defun set-eshell-prompt-function ()
  (setq eshell-prompt-function
         (lambda ()
            (propertize (format-time-string "%H:%M:%S" (current-time))
                        `(:foreground "#A0A0A0"))
            (propertize (user-login-name)
                        `(:foreground "#FF9080"))
            (propertize (let ((sys-name (system-name)))
                          (let ((sys-name-len (length sys-name)))
                            (if (> sys-name-len 8)
                                (concat (substring sys-name 0 8) "...")
                        `(:foreground "#FF9080"))
             (let ((limit 36)
                    (if (string= (eshell/pwd) (getenv "HOME"))
               (let ((loc-length (length loc)))
                 (if (> loc-length limit)
                     (concat "..." (substring loc (- loc-length limit)))
             `(:foreground "#FF9080"))
            "$ "))))
  (setq eshell-highlight-prompt t)
  (setq eshell-prompt-regexp "^[^#$\n]*[#$] "))


However, I would like to also see the last command's exit code in the prompt. I could not find the appropriate procedure to call to get it.

How can I get the last exit code, so that I can put it into the prompt?


At hint of the comment by Marioba, I have tried the following:

(propertize (concat "[" (number-to-string $?) "]")
                        `(:foreground "#A0A0A0"))

But unfortunately that does not work and results in the following error output in the *Messages* buffer:

concat: Symbol’s value as variable is void: $\? [2 times]
reference to free variable ‘$\?’
  • Maybe you can use $$ or $? variables. gnu.org/software/emacs/manual/html_mono/…).
    – Marioba
    Commented Sep 21, 2021 at 7:29
  • @Marioba Unfortunately that does not work, because these variables are not defined in the context of init.el. I edited my post to include the attempt. Thanks nevertheless! Commented Sep 21, 2021 at 12:44

1 Answer 1


How about using the eshell-last-command-status variable?

(setopt eshell-prompt-function
        (lambda ()
          (concat (abbreviate-file-name (eshell/pwd))
                  (if-let ((status eshell-last-command-status))
                      (if (= status 0) "" (format " [%s]" status)))
                  (if (= (user-uid) 0) " # " " $ "))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.