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
eval
may work just fine, but when the shell script exits then your parent shell won't notice. – Stephen Harris Feb 16 '23 at 02:41~/.profile
). – unrealapex Feb 16 '23 at 17:04