1

I am trying to automate adding Homebrew to my path in a shell script, but these two lines do not evaluate inside my shell script:

#!/bin/sh
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"

The second line of code runs the program [home]brew with the argument shellenv. It echoes several environment variables to set which are then evaluated. The first line just adds the second line to my ~/.profile so brew's PATH is setup in every shell.

I noticed that this statement does not work in my shell script, but it does when I type it into my terminal. What might be causing this?

I tried running the commands I was instructed to run after Homebrew installed in a shell script and got the same behavior.

similar question:

1 Answers1

1

Running the script creates a local environment, inherited from the invoking environment, in which the script is executing and which ultimately is destroyed when the script terminates.

I'm first assuming that by "does not evaluate" and "not working", you are observing that the brew shellenv command does not appear to be outputting anything when you run the script and that you, therefore, believe that the environment in the script is not initialised correctly for use with Homebrew. (NOTE: This interpretation is probably less correct after the question was updated with further clarifications. See after the divider below instead.)

The command brew shellenv only outputs the shell commands that set the appropriate environment variables for Homebrew if the necessary variables don't already have the correct values.

The documentation you get through brew help shellenv explains exactly what the command does. The following is from a macOS system, but it'll likely read similarly on Linux (my emphasis added):

Usage: brew shellenv

Print export statements. When run in a shell, this installation of Homebrew will be added to your PATH, MANPATH, and INFOPATH.

The variables HOMEBREW_PREFIX, HOMEBREW_CELLAR and HOMEBREW_REPOSITORY are also exported to avoid querying them multiple times. To help guarantee idempotence, this command produces no output when Homebrew's bin and sbin directories are first and second respectively in your PATH. Consider adding evaluation of this command's output to your dotfiles (e.g. ~/.profile, ~/.bash_profile, or ~/.zprofile) with: eval "$(brew shellenv)"

What may be happening is that your script is executing in an environment where the PATH variable already contains the bin and sbin directories, as described by the text above.


If you are instead trying to get your script to change the invoking shell's environment, you should be running your script with . or source instead, which would cause the commands in the file to be executed in the current environment (which also means that the #!-line would be ignored).

With regards to this, see also

Kusalananda
  • 333,661
  • Perhaps, but those directories are not present in my path, so I would assume not. My script does add the eval statement to my ~/.profile. I forgot to add that to my question. – unrealapex Feb 16 '23 at 17:28
  • I see, not sourcing it was the issue. Thank you for your help! – unrealapex Feb 19 '23 at 08:42