38 Commits

Author SHA1 Message Date
Xuehai Pan
6f0d87aace docs: prefer placing shellenv command in rcfile rather than shell profile on Linux 2023-10-07 00:14:26 +08:00
James Morris
793831cb93 fix eval (brew shellenv) from exiting with status 1 in fish
fish's `set` doesn't modify the exit status when assigning a variable.
This means that `set -q VAR; or set VAR ...` will return an exit status
of 1 even if the variable is suceessfully set. By switching the commands
to `! set -q VAR; and set VAR ...`, this prevents the 1 from propogating
when running `eval (brew shellenv)`.
2023-09-30 17:39:39 -04:00
Austin Ziegler
74b50ddc89
Fix small errors in brew shellenv
These were introduced by typos and an incorrect assumption on the last
update of #15358. The last commit had been tested with a parameter, but
had not been re-tested *without* parameter.

Apologies for the error.
2023-05-08 11:06:30 -04:00
Austin Ziegler
8719946036
Allow brew shellenv to accept a shell name param
Resolves #15358

With this change, `brew shellenv` will accept a shell name parameter to
override its default output (based on `/bin/ps`) for advanced use cases
such as with dotfile templating and caching with
[twpayne/chezmoi](https://github.com/twpayne/chezmoi/discussions/2971).
When running under `fish`, this is the output:

```console
$ brew shellenv pwsh
[System.Environment]::SetEnvironmentVariable('HOMEBREW_PREFIX','/opt/homebrew',[System.EnvironmentVariableTarget]::Process)
[System.Environment]::SetEnvironmentVariable('HOMEBREW_CELLAR','/opt/homebrew/Cellar',[System.EnvironmentVariableTarget]::Process)
[System.Environment]::SetEnvironmentVariable('HOMEBREW_REPOSITORY','/opt/homebrew',[System.EnvironmentVariableTarget]::Process)
[System.Environment]::SetEnvironmentVariable('PATH',$('/opt/homebrew/bin:/opt/homebrew/sbin:'+$ENV:PATH),[System.EnvironmentVariableTarget]::Process)
[System.Environment]::SetEnvironmentVariable('MANPATH',$('/opt/homebrew/share/man'+$(if(${ENV:MANPATH}){':'+${ENV:MANPATH}})+':'),[System.EnvironmentVariableTarget]::Process)
[System.Environment]::SetEnvironmentVariable('INFOPATH',$('/opt/homebrew/share/info'+$(if(${ENV:INFOPATH}){':'+${ENV:INFOPATH}})),[System.EnvironmentVariableTarget]::Process)
$ brew shellenv
set -gx HOMEBREW_PREFIX "/opt/homebrew";
set -gx HOMEBREW_CELLAR "/opt/homebrew/Cellar";
set -gx HOMEBREW_REPOSITORY "/opt/homebrew";
set -q PATH; or set PATH ''; set -gx PATH "/opt/homebrew/bin" "/opt/homebrew/sbin" $PATH;
set -q MANPATH; or set MANPATH ''; set -gx MANPATH "/opt/homebrew/share/man" $MANPATH;
set -q INFOPATH; or set INFOPATH ''; set -gx INFOPATH "/opt/homebrew/share/info" $INFOPATH;
```

The specific case presented in the mentioned discussion could be
mitigated by an additional level of indirection (`{{ output "fish" "-c"
"'brew shellenv'" | trim }}`), that requires that the running system
have the target shells installed, when they are not strictly necessary
with `brew shellenv`.
2023-05-05 11:50:48 -04:00
Adrian Ho
75c5769e75 shellenv: add explanatory MANPATH comment
Resolves #13867 and (hopefully) future attempts to correct the intended functionality.
2022-09-15 23:00:34 +08:00
Nanda H Krishna
b950ac3f6e
cmd/shellenv.sh: fix typo
Co-authored-by: Greg Myers <myersg86@gmail.com>
2022-01-15 17:27:49 -05:00
Carlo Cabrera
00137dda8e
Check that ${HOMEBREW_PREFIX}/bin is first in PATH 2021-12-09 20:50:47 +08:00
Carlo Cabrera
a62e88304b
shellenv: check that Homebrew's prefix precedes /usr/bin
Some setups (e.g. a default `tmux` session) will result in a user
starting multiple nested login shells. This is a problem for the way we
currently handle `shellenv` because a login shell invokes
`/usr/libexec/path_helper` which will prepend the contents of
`/etc/paths` and `/etc/paths.d` to `PATH`. In particular, the second
login shell will place `brew` further down the `PATH` than desired in a
non-`/usr/local` prefix, since `brew shellenv` will no longer produce
output the second time around.

Let's fix this by replacing the check that `brew` is in `PATH` with a
check that `${HOMEBREW_PREFIX}/bin` precedes `/usr/bin` in `PATH`.

Fixes #11851, #11883, Homebrew/discussions#2547.

See also:
- Homebrew/discussions#2237
- https://superuser.com/questions/544989/does-tmux-sort-the-path-variable
2021-12-09 20:50:22 +08:00
Julian Pawlowski
29f08603bf
Add support for PowerShell-preview package 2021-12-01 09:55:33 +01:00
Julian Pawlowski
cec3f2190a
Add shellenv template for PowerShell 2021-11-30 10:47:56 +01:00
XuehaiPan
bd8db0737d style: fix inconsistent code style for shell scripts 2021-09-16 01:06:19 +08:00
XuehaiPan
3f96d963f7 style: fix inconsistent code style for shell scripts 2021-09-15 14:59:01 +08:00
XuehaiPan
05c1c5895e Apply suggestions from code review 2021-08-30 22:37:07 +08:00
Xuehai Pan
27fb6506a0
Apply suggestions from code review
Co-authored-by: Carlo Cabrera <30379873+carlocab@users.noreply.github.com>
2021-08-27 18:22:01 +08:00
XuehaiPan
728efbdf2f Show warnings on brew shellenv if users modified env vars explicitly 2021-08-27 15:55:31 +08:00
XuehaiPan
1e737dbe2c cmd/shellenv.sh: apply suggestions from code review 2021-08-03 18:40:59 +08:00
XuehaiPan
6cd2192c6f cmd/shellenv.sh: make sure the current Homebrew is initialized correctly 2021-08-03 16:19:45 +08:00
XuehaiPan
7cc37fb7f8 cmd/shellenv.sh: support multi-instance installation 2021-08-02 16:57:59 +08:00
XuehaiPan
9e11fcf31e cmd/shellenv.sh: make brew shellenv idempotent 2021-07-30 21:00:38 +08:00
hyuraku
67c5006b0a cmd/shellenv.sh: repair style and set HOMEBREW_CASK_REPOSITORY 2021-04-28 20:47:39 +09:00
Z. Grace Moreau
a257d046ef remove spurious call to $fish_pid 2021-01-18 11:20:11 -07:00
Z. Grace Moreau
0dbd4157be query fish's PID correctly; also output executable names only 2021-01-17 14:25:09 -07:00
Tim Dorr
45368c8119 Add a default back to INFOPATH
It looks like #7738 removed the default unintentionally. This adds it back. 

For scripts that `set -u`, this will fail if $INFOPATH is not already set (`INFOPATH: unbound variable`). This provides an empty default, but one that will still satisfy the trailing colon requirement.
2020-07-19 13:37:52 +10:00
Adrian Ho
c166667666 shellenv: Fix shell detection
It currently switches on `$SHELL`, which points to the user's login shell. However, `shellenv` may be used in contexts where the running shell isn't `$SHELL` (e.g. csh-based `cron` scripts), and Linux desktop environments may set up user sessions in ways that trip up the current algorithm (see #7965 for an example). To work correctly in all circumstances and environments, its output should be determined by the context (calling shell) instead.

Fixes #7965.
2020-07-11 22:28:27 +08:00
Dan Kessler
30b04daa8a append colon (:) to end of brew's part of INFOPATH in brew shellenv
The behavior of some programs (e.g., emacs) differs depending on
whether INFOPATH is terminated in a colon or not. For example, the
info viewer built into emacs has a default list of places from which
it will draw info files, and it will only check this if either (a)
INFOPATH is unset, or (b) INFOPATH ends in a colon.

Currently brew shellenv prepends to the existing $INFOPATH and will
separate with a colon if $INFOPATH already exists, but if it does not
it will not terminate in a colon. As a result, info
pages (like emacs and elisp) are missing from the info viewer.

The existing brew shellenv logic used a parameter expansion trick to
include a colon only if it was prepending to an extant INFOPATH, but
because we want brew's contribution to the INFOPATH to always end in a
colon (whether there's an existing INFOPATH or not), we don't have to
use the parameter expansion trick

This commit only changes behavior for "other" shells (i.e., not fish,
csh, or tcsh) since I don't know how to append a colon to variables in
these shells.
2020-06-12 20:47:43 -04:00
April Arcus
0b4febceaf fix undefined variable interpolation in tcsh 2020-05-25 10:46:10 -07:00
Shaun Jackman
133ee7966d shell: Use .profile unless .bash_profile exists 2020-05-13 09:30:02 -07:00
Mike McQuaid
c05b3a755f
shellenv: recommend .bash_profile
Fixes #7516
2020-05-07 10:34:59 +01:00
Niklas Herder
2f4d4b8684
[fix] Improve shell detection in shellenv
$SHELL is set to only the binary name in some environments (e.g. fish, not /usr/bin/fish), causing detection of fish/csh/tsch to fail without this change.
2020-04-29 08:20:06 +02:00
Charles Gould
871974650b shellenv: use PATH variable, fish_user_paths should remain universal in scope 2020-03-26 09:38:25 -05:00
Chih-Pin Tan
7741672ecd Changes to be committed:
modified:   Library/Homebrew/cmd/shellenv.sh

Prevent overriding default MANPATH while MANPATH is empty.
2019-10-31 12:25:58 +00:00
Markus Reiter
59866d25ea Fix shellenv when using -u. 2019-10-17 20:14:01 +02:00
EricFromCanada
a9d1457c05 shellenv: suggest .zprofile for zsh users 2019-08-20 12:54:44 -04:00
EricFromCanada
8a8359cdd4 manpages: sentence rewording for readability
Use active voice when applicable, better verbs & adjectives, preposition usage, proper tense, etc.
2019-08-20 09:05:45 -04:00
なつき
193ead3c0c
fish 2.x support for brew shellenv 2019-02-21 17:54:29 -08:00
なつき
50ba2ad93f Support different shells for brew shellenv 2019-02-20 10:21:55 +00:00
Mike McQuaid
2a676dd94c
shellenv: improve --help formatting. 2019-01-30 21:38:45 +00:00
Mike McQuaid
44bcf69d1f
cmd/shellenv: use Bash.
This speeds up execution by 3x on my machine and the script is simple
enough to warrant this.
2018-09-13 09:27:44 +01:00