---
description: Standards for handling command paths in scripts, preferring PATH over absolute paths
globs: **/*.{sh,bash}
---
<rule>
When writing scripts that use external commands:

1. Always try PATH first:
   ```bash
   if command -v COMMAND >/dev/null 2>&1; then
       CMD="COMMAND"
   else
       CMD="/absolute/path/to/COMMAND"
   fi
   ```

2. Known fallback paths:
   - Bun: $HOME/.bun/bin/bun
   - Go: /usr/local/go/bin/go

3. Guidelines:
   - Use command -v to check if command exists in PATH
   - Store command path in a variable for reuse
   - Use meaningful variable names (e.g., BUN_CMD, GO_CMD)
   - Document why absolute paths are needed as fallback
   - Consider environment-specific paths
   - Use $HOME instead of ~ for home directory paths

4. Error Handling:
   - Always use `set -euo pipefail`
   - Wrap critical commands in error handlers:
     ```bash
     COMMAND || {
         echo "Command failed!"
         exit 1
     }
     ```
   - Add descriptive echo statements before and after important commands
   - Use meaningful exit codes
   - Ensure proper cleanup on error

4. Example pattern:
   ```bash
   # Find executable
   if command -v bun >/dev/null 2>&1; then
       BUN_CMD="bun"
   else
       BUN_CMD="$HOME/.bun/bin/bun"
   fi
   
   # Use the command
   $BUN_CMD run build
   ```

metadata:
  priority: high
  version: 1.0
</rule>

examples:
  - input: |
      # Bad: Using absolute path directly
      /usr/local/go/bin/go build
    output: |
      # Good: Try PATH first
      if command -v go >/dev/null 2>&1; then
          GO_CMD="go"
      else
          GO_CMD="/usr/local/go/bin/go"
      fi
      $GO_CMD build

  - input: |
      # Bad: No error handling
      go build -o app
    output: |
      # Good: With error handling
      echo "Building application..."
      go build -o app || {
          echo "Build failed!"
          exit 1
      }
      # Good: Use $HOME
      if command -v bun >/dev/null 2>&1; then
          BUN_CMD="bun"
      else
          BUN_CMD="$HOME/.bun/bin/bun"
      fi
      $BUN_CMD install