Skip to content

Conversation

@seefood
Copy link
Contributor

@seefood seefood commented Oct 4, 2025

Summary

Automatically sets up .bashrc sourcing during bash-it installation on macOS, BSD, Solaris, and Illumos systems.

This PR builds on the detection capability added in #2342 by implementing the auto-fix during installation.

Problem

On macOS/BSD/Solaris, login shells source .bash_profile or .profile, NOT .bashrc.

Without this fix, users would:

  1. Run ./install.sh successfully
  2. Open a new terminal or SSH session
  3. Find bash-it doesn't load 😞

Many users wouldn't know why or how to fix it.

Solution

During installation, automatically detect and fix .bashrc sourcing:

1. Platform Detection

  • Only runs on affected platforms (Darwin, SunOS, Illumos, *BSD)
  • Linux users unaffected (no extra prompts)

2. Smart Detection

3. Interactive Prompt

  • Prompts: "Would you like to add .bashrc sourcing to [file]? [Y/n]"
  • Shows manual instructions if user declines
  • Silent mode (-s flag): auto-accepts (safe default)

4. Safe Modifications

  • Backs up profile file with timestamp before modifying
  • Uses command cp to bypass user aliases (defensive programming)
  • Creates .bash_profile if no profile file exists
  • Appends standard BASH_VERSION check snippet

User Experience

Before (macOS user):

$ ./install.sh
Installation finished successfully!
$ # opens new terminal... bash-it doesn't work! 😞

After (macOS user):

$ ./install.sh
...
Warning: .bashrc is not sourced from /Users/me/.bash_profile
On macOS/BSD/Solaris, login shells won't load bash-it without this.

Would you like to add .bashrc sourcing to /Users/me/.bash_profile? [Y/n] y
Backed up /Users/me/.bash_profile to /Users/me/.bash_profile.bak.20251004_193245
✓ Added .bashrc sourcing to /Users/me/.bash_profile

Installation finished successfully!
$ # opens new terminal... bash-it works! 😃

Silent mode:

$ ./install.sh -s
...
✓ Added .bashrc sourcing to /Users/me/.bash_profile
Installation finished successfully!

Code Snippet Added

The installer adds this standard snippet to the profile file:

# Source .bashrc if running bash
if [ -n "$BASH_VERSION" ]; then
	if [ -f "$HOME/.bashrc" ]; then
		. "$HOME/.bashrc"
	fi
fi

Implementation Details

  • Reuses detection function from doctor (DRY principle)
  • Defensive programming: command cp to bypass aliases
  • Timestamp-based backups prevent overwrites
  • Shellcheck compliant
  • Silent mode compatible

Testing

Closes

Closes #1455 (Solaris/Illumos support)

Related


🤖 Generated with Claude Code

Co-Authored-By: Claude [email protected]

Implements the auto-fix portion of issue Bash-it#1455 (Solaris/Illumos support).

**Problem:**
On macOS, BSD, Solaris, and Illumos, login shells source .bash_profile or
.profile, NOT .bashrc. Users would install bash-it successfully but find it
doesn't load in:
- Terminal.app (macOS default terminal)
- SSH sessions
- Any new login shell

**Solution:**
During installation, automatically detect and fix .bashrc sourcing:

1. **Platform Detection:**
   - Only runs on affected platforms (Darwin, SunOS, Illumos, *BSD)
   - Linux users unaffected (no extra prompts)

2. **Smart Detection:**
   - Reuses `_bash-it-doctor-check-profile-sourcing-grep()` from doctor
   - Fast grep-based detection of existing sourcing patterns
   - Skips if already configured

3. **Interactive Prompt:**
   - Prompts user: "Would you like to add .bashrc sourcing? [Y/n]"
   - Shows manual instructions if user declines
   - Silent mode: auto-accepts (safe default behavior)

4. **Safe Modifications:**
   - Backs up profile file with timestamp before modifying
   - Uses `command cp` to bypass aliases
   - Creates .bash_profile if no profile file exists
   - Appends standard BASH_VERSION check snippet

**User Experience:**

Before (macOS user):
```
$ bash-it install
Installation finished successfully!
$ # opens new terminal... bash-it doesn't work! 😞
```

After (macOS user):
```
$ bash-it install
...
Warning: .bashrc is not sourced from /Users/me/.bash_profile
Would you like to add .bashrc sourcing? [Y/n] y
✓ Added .bashrc sourcing to /Users/me/.bash_profile
Installation finished successfully!
$ # opens new terminal... bash-it works! 😃
```

**Testing:**
- ✅ Shellcheck passes
- ✅ Detection logic tested
- ✅ Silent mode compatible
- ✅ Handles missing profile files

Closes Bash-it#1455

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
Apply defensive shell scripting best practice to prevent errors when
`set -u` (nounset) is active in user shells. Replace `$BASH_VERSION`
with `${BASH_VERSION-}` in both:
- Manual instructions shown to users
- Auto-generated .bash_profile snippet

This ensures the check works correctly even when BASH_VERSION is unset
(e.g., when running under non-bash shells like dash or sh).

Also documents this pattern in CLAUDE.md for future development.

Addresses review feedback from @akinomyoga in PR Bash-it#2343

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@seefood
Copy link
Contributor Author

seefood commented Oct 4, 2025

Fixed the ${BASH_VERSION-} usage in 9ec149a per review feedback. Also documented this pattern in CLAUDE.md for future reference. Thanks @akinomyoga!

@seefood seefood merged commit 9e62a5b into Bash-it:master Oct 4, 2025
6 checks passed
@seefood seefood deleted the feature/bashrc-sourcing-detection branch October 4, 2025 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for Solaris & Illumos etc. (*easy* fix, code suggestion within)

2 participants