6

When launched at system start-up, the Emacs daemon doesn't pick up modifications to the PATH introduced by shell initialization scripts.

Emacswiki suggests to use this function to update Emacs' PATH,

(defun set-exec-path-from-shell-PATH ()
  "Set up Emacs' `exec-path' and PATH environment variable to match
that used by the user's shell.

This is particularly useful under Mac OS X and macOS, where GUI
apps are not started from a shell."
  (interactive)
  (let ((path-from-shell (replace-regexp-in-string
              "[ \t\n]*$" "" (shell-command-to-string
                      "$SHELL --login -i -c 'echo $PATH'"
                            ))))
    (setenv "PATH" path-from-shell)
    (setq exec-path (split-string path-from-shell path-separator))))

(set-exec-path-from-shell-PATH)

but it returns some errors:

bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell
stty: 'standard input': Inappropriate ioctl for device

As far as I understand (my main reference), since I want to run Bash in interactive mode (so that it loads ~/.bashrc) I should use start-file-process(-shell-command) to invoke the shell process. However, I can't figure out how to store the output of the command in a string when I do so.

Arch Stanton
  • 1,525
  • 9
  • 22
  • I fixed the doc in the [Emacswiki](https://www.emacswiki.org/emacs/ExecPath) and opened an issue on the [source of the misinformation](https://github.com/Wilfred/ag.el/issues/172), – NickD Mar 24 '21 at 03:45
  • Good Q and A. Thanks for fixing that wiki page, @NickD – Drew Mar 24 '21 at 04:29
  • There is the package exec-path-from-shell - also see Aquamacs code for another way – mmmmmm Mar 24 '21 at 10:52

1 Answers1

8

No, you don’t want to use -i because you’re not launching an interactive shell. That is, this shell will not be connected to a terminal that the user can type in. It is instead going to be connected to a pipe so that Emacs can read whatever it prints. Removing the -i option will prevent Bash from trying to use inappropriate ioctls on the stdout and stdin, and thus eliminate the error messages.

If your PATH is only configured by your ~/.bashrc file, then you’ve misconfigured Bash. You should ensure that at least your PATH is set up consistently in both interactive and non—interactive shells. Most distros do this by making the default ~/.bash_profile source ~/.bashrc for you. Configuring this correctly will ensure that you get the correct PATH value.

db48x
  • 15,741
  • 1
  • 19
  • 23
  • how do I configure this correctly? – xdavidliu May 27 '22 at 22:30
  • Configure it correcty by setting PATH in `~/.bash_profile` instead of `~/.bashrc`. That way your PATH variable is correctly set in both interactive and non–interactive shells. – db48x May 28 '22 at 12:22