diff --git a/CLAUDE.md b/CLAUDE.md index 2f6731f142..a240cb109f 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -140,3 +140,9 @@ bash-it search docker - `command rm` instead of `rm` (users may have `alias rm='rm -i'`) - Apply to any command that could be aliased and break core functionality - This prevents surprises from user's alias configurations in bash-it core functions +- **Use parameter expansion with default for potentially unset variables**: + - `${VARIABLE-}` instead of `$VARIABLE` when variable may be unset + - Prevents errors when `set -u` is active in user's shell + - Examples: `${BASH_VERSION-}`, `${HOME-}`, `${PATH-}` + - Critical for variables checked in conditionals: `if [ -n "${BASH_VERSION-}" ]` + - This defensive practice ensures scripts work regardless of user's shell options diff --git a/install.sh b/install.sh index 392eef3328..a99993e4ef 100755 --- a/install.sh +++ b/install.sh @@ -41,6 +41,79 @@ function _bash-it-install-enable() { done } +# Ensure .bashrc is sourced from profile files on macOS/BSD/Solaris +function _bash-it-install-ensure-bashrc-sourcing() { + # Only needed on platforms where login shells don't source .bashrc + case "$(uname -s)" in + Darwin | SunOS | Illumos | *BSD) ;; + *) return 0 ;; # Not needed on Linux + esac + + # Find which profile file exists + local profile_file + if [[ -f "$HOME/.bash_profile" ]]; then + profile_file="$HOME/.bash_profile" + elif [[ -f "$HOME/.profile" ]]; then + profile_file="$HOME/.profile" + else + # No profile file exists, create .bash_profile + profile_file="$HOME/.bash_profile" + echo -e "${echo_yellow:-}Creating $profile_file to source .bashrc${echo_normal:-}" + fi + + # Check if already sourced (reuse helper from doctor) + if _bash-it-doctor-check-profile-sourcing-grep "$profile_file" 2> /dev/null; then + return 0 # Already configured + fi + + # Not sourced, offer to add it + echo "" + echo -e "${echo_yellow:-}Warning: .bashrc is not sourced from $profile_file${echo_normal:-}" + echo "On macOS/BSD/Solaris, login shells won't load bash-it without this." + echo "" + + local RESP + if [[ -z "${silent:-}" ]]; then + read -r -e -n 1 -p "Would you like to add .bashrc sourcing to $profile_file? [Y/n] " RESP + case $RESP in + [nN]) + echo "" + echo -e "${echo_orange:-}Skipping. You can add this manually later:${echo_normal:-}" + echo "" + echo " if [ -n \"\${BASH_VERSION-}\" ]; then" + echo " if [ -f \"\$HOME/.bashrc\" ]; then" + echo " . \"\$HOME/.bashrc\"" + echo " fi" + echo " fi" + echo "" + return 0 + ;; + esac + fi + + # Backup profile file if it exists + if [[ -f "$profile_file" ]]; then + local backup_file + backup_file="${profile_file}.bak.$(date +%Y%m%d_%H%M%S)" + command cp "$profile_file" "$backup_file" + echo -e "${echo_green:-}Backed up $profile_file to $backup_file${echo_normal:-}" + fi + + # Add the sourcing snippet + cat >> "$profile_file" << 'EOF' + +# Source .bashrc if running bash +if [ -n "${BASH_VERSION-}" ]; then + if [ -f "$HOME/.bashrc" ]; then + . "$HOME/.bashrc" + fi +fi +EOF + + echo -e "${echo_green:-}✓ Added .bashrc sourcing to $profile_file${echo_normal:-}" + echo "" +} + # Back up existing profile function _bash-it-install-backup-config() { test -w "${HOME?}/${CONFIG_FILE?}" \ @@ -222,6 +295,9 @@ else _bash-it-profile-load "default" fi +# Ensure .bashrc sourcing is set up on macOS/BSD/Solaris +_bash-it-install-ensure-bashrc-sourcing + echo "" echo -e "${echo_green:-}Installation finished successfully! Enjoy bash-it!${echo_normal:-}" echo -e "${echo_green:-}To start using it, open a new tab or 'source ~/${CONFIG_FILE?}'.${echo_normal:-}"