Skip to content

Commit 9e62a5b

Browse files
authored
Merge pull request #2343 from seefood/feature/bashrc-sourcing-detection
2 parents e98418c + 9ec149a commit 9e62a5b

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

CLAUDE.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,9 @@ bash-it search docker
140140
- `command rm` instead of `rm` (users may have `alias rm='rm -i'`)
141141
- Apply to any command that could be aliased and break core functionality
142142
- This prevents surprises from user's alias configurations in bash-it core functions
143+
- **Use parameter expansion with default for potentially unset variables**:
144+
- `${VARIABLE-}` instead of `$VARIABLE` when variable may be unset
145+
- Prevents errors when `set -u` is active in user's shell
146+
- Examples: `${BASH_VERSION-}`, `${HOME-}`, `${PATH-}`
147+
- Critical for variables checked in conditionals: `if [ -n "${BASH_VERSION-}" ]`
148+
- This defensive practice ensures scripts work regardless of user's shell options

install.sh

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,79 @@ function _bash-it-install-enable() {
4141
done
4242
}
4343

44+
# Ensure .bashrc is sourced from profile files on macOS/BSD/Solaris
45+
function _bash-it-install-ensure-bashrc-sourcing() {
46+
# Only needed on platforms where login shells don't source .bashrc
47+
case "$(uname -s)" in
48+
Darwin | SunOS | Illumos | *BSD) ;;
49+
*) return 0 ;; # Not needed on Linux
50+
esac
51+
52+
# Find which profile file exists
53+
local profile_file
54+
if [[ -f "$HOME/.bash_profile" ]]; then
55+
profile_file="$HOME/.bash_profile"
56+
elif [[ -f "$HOME/.profile" ]]; then
57+
profile_file="$HOME/.profile"
58+
else
59+
# No profile file exists, create .bash_profile
60+
profile_file="$HOME/.bash_profile"
61+
echo -e "${echo_yellow:-}Creating $profile_file to source .bashrc${echo_normal:-}"
62+
fi
63+
64+
# Check if already sourced (reuse helper from doctor)
65+
if _bash-it-doctor-check-profile-sourcing-grep "$profile_file" 2> /dev/null; then
66+
return 0 # Already configured
67+
fi
68+
69+
# Not sourced, offer to add it
70+
echo ""
71+
echo -e "${echo_yellow:-}Warning: .bashrc is not sourced from $profile_file${echo_normal:-}"
72+
echo "On macOS/BSD/Solaris, login shells won't load bash-it without this."
73+
echo ""
74+
75+
local RESP
76+
if [[ -z "${silent:-}" ]]; then
77+
read -r -e -n 1 -p "Would you like to add .bashrc sourcing to $profile_file? [Y/n] " RESP
78+
case $RESP in
79+
[nN])
80+
echo ""
81+
echo -e "${echo_orange:-}Skipping. You can add this manually later:${echo_normal:-}"
82+
echo ""
83+
echo " if [ -n \"\${BASH_VERSION-}\" ]; then"
84+
echo " if [ -f \"\$HOME/.bashrc\" ]; then"
85+
echo " . \"\$HOME/.bashrc\""
86+
echo " fi"
87+
echo " fi"
88+
echo ""
89+
return 0
90+
;;
91+
esac
92+
fi
93+
94+
# Backup profile file if it exists
95+
if [[ -f "$profile_file" ]]; then
96+
local backup_file
97+
backup_file="${profile_file}.bak.$(date +%Y%m%d_%H%M%S)"
98+
command cp "$profile_file" "$backup_file"
99+
echo -e "${echo_green:-}Backed up $profile_file to $backup_file${echo_normal:-}"
100+
fi
101+
102+
# Add the sourcing snippet
103+
cat >> "$profile_file" << 'EOF'
104+
105+
# Source .bashrc if running bash
106+
if [ -n "${BASH_VERSION-}" ]; then
107+
if [ -f "$HOME/.bashrc" ]; then
108+
. "$HOME/.bashrc"
109+
fi
110+
fi
111+
EOF
112+
113+
echo -e "${echo_green:-}✓ Added .bashrc sourcing to $profile_file${echo_normal:-}"
114+
echo ""
115+
}
116+
44117
# Back up existing profile
45118
function _bash-it-install-backup-config() {
46119
test -w "${HOME?}/${CONFIG_FILE?}" \
@@ -222,6 +295,9 @@ else
222295
_bash-it-profile-load "default"
223296
fi
224297

298+
# Ensure .bashrc sourcing is set up on macOS/BSD/Solaris
299+
_bash-it-install-ensure-bashrc-sourcing
300+
225301
echo ""
226302
echo -e "${echo_green:-}Installation finished successfully! Enjoy bash-it!${echo_normal:-}"
227303
echo -e "${echo_green:-}To start using it, open a new tab or 'source ~/${CONFIG_FILE?}'.${echo_normal:-}"

0 commit comments

Comments
 (0)