Skip to content

Instantly share code, notes, and snippets.

@krshubham
Created February 23, 2026 17:01
Show Gist options
  • Select an option

  • Save krshubham/f8854f08980ef55abb71b91069587cdc to your computer and use it in GitHub Desktop.

Select an option

Save krshubham/f8854f08980ef55abb71b91069587cdc to your computer and use it in GitHub Desktop.
Play Faaah when something goes wrong in your terminal
#!/usr/bin/env bash
# =============================================================================
# faaah.sh — Play the FAAAH sound whenever a command (or test suite) fails
# =============================================================================
# Works with : Bash & Zsh | macOS & Linux
# Sound file : ~/.sounds/faaah.mp3 (override via $FAAAH_SOUND)
#
# ┌─ ONE-LINE INSTALL ──────────────────────────────────────────────────────┐
# │ │
# │ bash <(curl -fsSL https://gist.githubusercontent.com/anonymous/ │
# │ e859a68aea5b2fa0ff538360373678f4/raw/faaah.sh) --install │
# │ │
# └──────────────────────────────────────────────────────────────────────────┘
#
# Works automatically with: npm test · pytest · cargo test · go test ·
# make · jest · mocha · rspec · gradle test · mvn test … any exit ≠ 0.
# =============================================================================
FAAAH_SOUND="${FAAAH_SOUND:-$HOME/.sounds/faaah.mp3}"
FAAAH_GIST_RAW="https://gist.githubusercontent.com/anonymous/e859a68aea5b2fa0ff538360373678f4/raw/faaah.sh"
FAAAH_MP3_URL="https://www.myinstants.com/media/sounds/faaah.mp3"
# ── Audio player (auto-detected) ─────────────────────────────────────────────
_faaah_play() {
[[ ! -f "$FAAAH_SOUND" ]] && return
if command -v afplay &>/dev/null; then (afplay "$FAAAH_SOUND" &>/dev/null &)
elif command -v mpg123 &>/dev/null; then (mpg123 -q "$FAAAH_SOUND" &>/dev/null &)
elif command -v ffplay &>/dev/null; then (ffplay -nodisp -autoexit -loglevel quiet "$FAAAH_SOUND" &>/dev/null &)
elif command -v paplay &>/dev/null; then (paplay "$FAAAH_SOUND" &>/dev/null &)
fi
}
# ── Hook fires after every command ───────────────────────────────────────────
_faaah_hook() {
local exit_code=$?
[[ $exit_code -ne 0 ]] && _faaah_play
}
# ── Register hook for the current shell ──────────────────────────────────────
_faaah_register() {
if [[ -n "$ZSH_VERSION" ]]; then
autoload -Uz add-zsh-hook 2>/dev/null
if typeset -f add-zsh-hook &>/dev/null; then
add-zsh-hook precmd _faaah_hook
else
precmd_functions+=(_faaah_hook)
fi
elif [[ -n "$BASH_VERSION" ]]; then
if [[ -z "$PROMPT_COMMAND" ]]; then
PROMPT_COMMAND="_faaah_hook"
elif [[ "$PROMPT_COMMAND" != *"_faaah_hook"* ]]; then
PROMPT_COMMAND="_faaah_hook; $PROMPT_COMMAND"
fi
fi
}
# ── Download the mp3 ─────────────────────────────────────────────────────────
_faaah_download_sound() {
mkdir -p "$(dirname "$FAAAH_SOUND")"
echo " Downloading faaah.mp3 ..."
if command -v curl &>/dev/null; then
curl -fsSL "$FAAAH_MP3_URL" -o "$FAAAH_SOUND"
elif command -v wget &>/dev/null; then
wget -q "$FAAAH_MP3_URL" -O "$FAAAH_SOUND"
else
echo " x curl/wget not found. Download manually:"
echo " $FAAAH_MP3_URL -> $FAAAH_SOUND"
return 1
fi
echo " Sound saved to $FAAAH_SOUND"
}
# ── One-liner installer ───────────────────────────────────────────────────────
_faaah_install() {
echo ""
echo " faaah installer"
echo " ==============="
# Detect shell rc
local rc=""
if [[ "$SHELL" == */zsh ]] || [[ -n "$ZSH_VERSION" ]]; then
rc="$HOME/.zshrc"
elif [[ "$SHELL" == */bash ]] || [[ -n "$BASH_VERSION" ]]; then
rc="$HOME/.bashrc"
else
echo " x Unknown shell: $SHELL"
echo " Manually add to your shell rc: source <(curl -fsSL $FAAAH_GIST_RAW)"
return 1
fi
# Download sound
if [[ ! -f "$FAAAH_SOUND" ]]; then
_faaah_download_sound || true
else
echo " Sound file already exists at $FAAAH_SOUND"
fi
# Add source line to shell rc
local source_line="source <(curl -fsSL $FAAAH_GIST_RAW)"
if grep -qF "faaah" "$rc" 2>/dev/null; then
echo " Already present in $rc -- nothing to add."
else
{
echo ""
echo "# faaah -- play sound on non-zero exit / test failure"
echo "$source_line"
} >> "$rc"
echo " Added to $rc"
fi
# Check for audio player on Linux
if [[ "$(uname)" == "Linux" ]]; then
if ! command -v mpg123 &>/dev/null && ! command -v ffplay &>/dev/null && ! command -v paplay &>/dev/null; then
echo ""
echo " WARNING: No audio player found. Install one:"
echo " sudo apt install mpg123 # Debian/Ubuntu"
echo " sudo dnf install mpg123 # Fedora"
echo " sudo pacman -S mpg123 # Arch"
fi
fi
echo ""
echo " Done! Restart your shell or run:"
echo " source $rc"
echo ""
echo " Test it with: false"
echo ""
}
# ── Entry point ───────────────────────────────────────────────────────────────
if [[ "${1}" == "--install" ]]; then
_faaah_install
else
# Being sourced normally -- just register the hook
_faaah_register
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment