diff --git a/cast.sh b/cast.sh index 17bf79e..c9e5eeb 100644 --- a/cast.sh +++ b/cast.sh @@ -145,9 +145,27 @@ install_plugins() { # 🐚 Ask to set Fish as default shell (only if not already) set_fish_default() { - local fish_path current_shell + local fish_path current_shell confirm + fish_path="$(command -v fish)" - current_shell="$(getent passwd "$USER" | cut -d: -f7)" + + if [[ -z "$fish_path" ]]; then + echo "✗ Fish is not installed or not in PATH" + return 1 + fi + + # Detect the user's configured login shell in a cross-platform way + if command -v getent >/dev/null 2>&1; then + # Linux / glibc systems + current_shell="$(getent passwd "$USER" | cut -d: -f7)" + elif command -v dscl >/dev/null 2>&1; then + # macOS + current_shell="$(dscl . -read "/Users/$USER" UserShell 2>/dev/null | awk '{print $2}')" + else + # Fallback: try /etc/passwd, then SHELL env var + current_shell="$(awk -F: -v user="$USER" '$1 == user {print $7}' /etc/passwd 2>/dev/null)" + [[ -z "$current_shell" ]] && current_shell="$SHELL" + fi if [[ "$current_shell" == "$fish_path" ]]; then echo "✓ Fish is already your default shell" @@ -156,11 +174,19 @@ set_fish_default() { read -rp "Set Fish as your default shell? (y/N): " confirm if [[ "$confirm" =~ ^[Yy]$ ]]; then - if ! grep -q "^$fish_path$" /etc/shells; then - echo "$fish_path" | sudo tee -a /etc/shells >/dev/null + # Only try to add to /etc/shells if that file exists + if [[ -f /etc/shells ]]; then + if ! grep -qx "$fish_path" /etc/shells; then + echo "$fish_path" | sudo tee -a /etc/shells >/dev/null + fi + fi + + if chsh -s "$fish_path"; then + echo "✓ Fish set as default shell (applies to new sessions)" + else + echo "✗ Failed to set Fish as default shell" + return 1 fi - chsh -s "$fish_path" - echo "✓ Fish set as default shell (applies to new sessions)" else echo "✓ Keeping current default shell" fi