postmodern / chruby

Changes the current Ruby
MIT License
2.85k stars 190 forks source link

Fix the instruction for macOS about Bash #473

Closed FranklinYu closed 2 years ago

FranklinYu commented 2 years ago

This is a follow-up on #437 and #438.

background knowledge for Bash

If you run a Bash within a Bash, you are in a subshell. For example, if you execute bash after logging in, the shell process structure is like this:

bash (0, root)
 └── bash (1, subshell)

Then Bash No.0 would be the “interactive, login” shell, loading ~/.bash_profile (falling back to ~/.profile for compatibility) but not ~/.bashrc. In contrast, Bash No.1 would be an “interactive, non-login” shell, loading ~/.bashrc but none of the profiles. Things are simpler in Linux workstation because Desktop Environments (GNOME, KDE Plasma, etc.) are typically the login shell, so all the Bash in terminal emulators (like GNOME Terminal) are non-login shells reading ~/.bashrc as expected. macOS is the weird guy: every new tab in the terminal (both Terminal.app and iTerm2) is a login shell (looks like a new session). This macOS issue isn’t Bash-specific, but Zsh loads ~/.zshrc regardless of the “login shell” bit, so Zsh users don’t even feel it.

The official solution, is to source ~/.bashrc in ~/.bash_profile, for a few reasons:

  1. This way, stuffs necessary for interactive use (like chruby) only need to specify once. Less typo, no need to synchronize rc-file and profile.
  2. This setup is portable; it works on both Linux and macOS.
  3. Unlike ~/.profile, ~/.bash_profile is Bash-specific (hence the name), so we don’t need to test whether in Bash. As a result, this won’t affect other POSIX shells (or shells that may or may not read ~/.profile).
  4. Users can put some POSIX stuffs in ~/.profile, shared by various POSIX shells, if desired.

other notes

FranklinYu commented 2 years ago

@peterb Sorry for taking more than a year to do it! It went out of my radar.

peterb commented 2 years ago

Looks very nice! I think this will help people using Bash on OS X. Does [[ $- == *i* ]] check if what's running is an interactive shell? Maybe worth mentioning that. By the way I started using ZSH on OS X after the conversations we had.

The description of this MR looks good for a Blog post.

postmodern commented 2 years ago

Very thorough explanation!