Powershell: /usr/local/bin/pwsh should be registered as a shell in /etc/shells on mac as it is on linux

Created on 25 Jan 2018  路  6Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

chsh -s /usr/local/bin/pwsh

Expected behavior

pwsh should become your shell

Actual behavior

chsh: /usr/local/bin/pwsh: non-standard shell

bash stays your shell because pwsh is not registered as a shell

Environment data

> $PSVersionTable
Name                           Value                                           
----                           -----                                           
PSVersion                      6.0.0                                           
PSEdition                      Core                                            
GitCommitId                    v6.0.1                                          
OS                             Darwin 17.3.0 Darwin Kernel Version 17.3.0: T...
Platform                       Unix                                            
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                         
PSRemotingProtocolVersion      2.3                                             
SerializationVersion           1.1.0.1                                         
WSManStackVersion              3.0                                             
Issue-Enhancement OS-macOS

All 6 comments

@mklement0 Correct me if I'm wrong, but, don't we have to avoid using pwsh as a login shell for various reasons?

I've found at least one on mac... the environment doesn't get loaded properly...

975

@TravisEz13:

If you use sudo chsh -s ..., you'll change the shell for the _root_ user; please use just
chsh -s /usr/local/bin/pwsh.

For ad-hoc use, here's an idempotent command for adding pwsh to the list of permissible default shells:

grep -x /usr/local/bin/pwsh /etc/shells || sudo sh -c 'echo /usr/local/bin/pwsh >> /etc/shells'

@markekraus: I'll address the other issues soon.

In general, using pwsh as one's default shell works fine on macOS (I've only tested superficially), _except_ - and that's a big except - for the fact that it doesn't process /etc/profile, where important system-wide initializations happen.

The previously discussed issue is that /etc/profile cannot be read directly by PowerShell, because it is coded for POSIX-compliant shells (sh; as an aside, note that the language constructs therefore permitted in that file are only a _subset_ of what bash supports).

On macOS specifically, the only important thing that /etc/profile does (at least by default) is to add important entries to $env:PATH

Fortunately, that is done via an _external utility_, /usr/libexec/path_helper, which you can also call from PS; e.g., by placing the following in your $PROFILE file (note the need for extra measures, given that $PROFILE is sourced for _every_ PowerShell instance by default):

  if ($IsMacOS -and $env:PATH -notmatch '\b/usr/local/bin\b') { # prevent repeated replacement of $env:PATH
    & { # Use a child scope, so that helper function setenv() doesn't linger.
      function setenv ($variable, $value) { [Environment]::SetEnvironmentVariable($variable, $value)  }
      # `/usr/libexec/path_helper -c` conveniently outputs something like 'setenv PATH "/usr/local/bin:..."',
      # which we can pass to Invoke-Expression, which then calls our transient `setenv()` function.
      Invoke-Expression (/usr/libexec/path_helper -c)  
    }
  }

Also, _GUI apps_ on macOS do NOT rely on /etc/profile; the latter's importance seems limited to _shells_.

On Linux distros, this can get trickier *, where *the entire system _may_ depend on initializations performed in /etc/profile (I have not delved into this), and where important $env:PATH additions are _not_ conveniently obtainable from a single utility call. Most likely not a problem, after all - see link below.

Comment https://github.com/PowerShell/PowerShell/issues/975#issuecomment-331049792 from the (lengthy) linked issue tries to summarize the problem.

I updated the issue with more details on the actual case and removed sudo from the repro as @markekraus suggested.

I think I agree with @mklement0 that the shell is mostly usable and I would like to easily be able to switch after I install. Even more so, now that I have the utility that @mklement0 suggested.

Was this page helpful?
0 / 5 - 0 ratings