Skip to content

Instantly share code, notes, and snippets.

@dipta007
Last active January 2, 2026 03:36
Show Gist options
  • Select an option

  • Save dipta007/1e464bc94c7da17b45d31b8a7cad8bde to your computer and use it in GitHub Desktop.

Select an option

Save dipta007/1e464bc94c7da17b45d31b8a7cad8bde to your computer and use it in GitHub Desktop.
Using claude cli to imitate `aichat -e`
#!/bin/bash
# claude-exec: Shell assistant using Claude Code CLI
# Usage: claude-exec [options] <natural language description>
#
# Options:
# -h, --help Show help
# -c, --copy Copy command to clipboard instead of executing
# -y, --yes Execute without confirmation
# -v, --verbose Show system info being sent to Claude
#
# Examples:
# claude-exec "find all python files modified in the last week"
# claude-exec -y "show disk usage"
# claude-exec -c "compress this folder"
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
GRAY='\033[0;90m'
NC='\033[0m' # No Color
BOLD='\033[1m'
# Options
AUTO_EXECUTE=false
COPY_MODE=false
VERBOSE=false
# Parse options
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
echo "claude-exec: Shell assistant using Claude Code CLI"
echo ""
echo "Usage: claude-exec [options] <natural language description>"
echo ""
echo "Options:"
echo " -h, --help Show this help"
echo " -c, --copy Copy command to clipboard instead of executing"
echo " -y, --yes Execute without confirmation"
echo " -v, --verbose Show system info being sent to Claude"
echo ""
echo "Examples:"
echo " claude-exec \"find all python files modified in the last week\""
echo " claude-exec -y \"show disk usage\""
echo " claude-exec -c \"compress this folder\""
echo ""
echo "Interactive commands:"
echo " [e]xecute - Run the command"
echo " [a]bort - Cancel"
echo " [r]evise - Edit the command before running"
echo " [?]explain - Ask Claude to explain the command"
echo " [c]opy - Copy command to clipboard"
exit 0
;;
-c|--copy)
COPY_MODE=true
shift
;;
-y|--yes)
AUTO_EXECUTE=true
shift
;;
-v|--verbose)
VERBOSE=true
shift
;;
-*)
echo -e "${RED}Unknown option: $1${NC}"
echo "Use -h for help"
exit 1
;;
*)
break
;;
esac
done
# Check if claude CLI is available
if ! command -v claude &> /dev/null; then
echo -e "${RED}Error: claude CLI not found.${NC}"
echo "Please install Claude Code: https://docs.anthropic.com/en/docs/claude-code"
exit 1
fi
# Check if input provided
if [ $# -eq 0 ]; then
echo -e "${CYAN}Usage:${NC} claude-exec [options] <natural language description>"
echo -e "${GRAY}Use -h for full help${NC}"
exit 1
fi
# Gather system context
OS_TYPE=$(uname -s)
OS_RELEASE=""
ARCH=$(uname -m)
if [ -f /etc/os-release ]; then
OS_RELEASE=$(grep PRETTY_NAME /etc/os-release 2>/dev/null | cut -d'"' -f2)
elif [ "$OS_TYPE" = "Darwin" ]; then
OS_RELEASE="macOS $(sw_vers -productVersion 2>/dev/null || echo 'unknown')"
fi
SHELL_NAME=$(basename "$SHELL")
CURRENT_DIR=$(pwd)
USER_NAME=$(whoami)
# Get directory listing context (first 20 items)
DIR_CONTENTS=$(ls -1 2>/dev/null | head -20 | tr '\n' ', ' | sed 's/,$//')
# Build the prompt
USER_REQUEST="$*"
SYSTEM_INFO="System Information:
- OS: ${OS_TYPE} ${OS_RELEASE} (${ARCH})
- Shell: ${SHELL_NAME}
- Current directory: ${CURRENT_DIR}
- Directory contents: ${DIR_CONTENTS}
- User: ${USER_NAME}"
if [ "$VERBOSE" = true ]; then
echo -e "${GRAY}${SYSTEM_INFO}${NC}"
echo ""
fi
PROMPT="You are a shell command generator. Generate a shell command for the following request.
${SYSTEM_INFO}
Request: ${USER_REQUEST}
Rules:
1. Output ONLY the shell command, nothing else
2. No explanations, no markdown, no code blocks, no backticks
3. Use commands appropriate for the detected OS and shell
4. If multiple commands are needed, chain them appropriately (&&, |, ;)
5. Prefer common/portable commands when possible
6. For destructive operations, do NOT add confirmation flags - the user will confirm separately
7. If referring to files in current directory, use the directory contents for context"
# Function to copy to clipboard
copy_to_clipboard() {
local text="$1"
if [ "$OS_TYPE" = "Darwin" ]; then
echo -n "$text" | pbcopy
return 0
elif command -v xclip &> /dev/null; then
echo -n "$text" | xclip -selection clipboard
return 0
elif command -v xsel &> /dev/null; then
echo -n "$text" | xsel --clipboard
return 0
elif command -v wl-copy &> /dev/null; then
echo -n "$text" | wl-copy
return 0
fi
return 1
}
# Function to explain command
explain_command() {
local cmd="$1"
echo -e "${BLUE}⟳ Getting explanation...${NC}"
local explain_prompt="Explain this shell command concisely (2-3 sentences max): $cmd"
claude -p "$explain_prompt" --model haiku 2>/dev/null
echo ""
}
# Get command from Claude
echo -e "${BLUE}⟳ Generating command...${NC}"
GENERATED_CMD=$(claude -p "$PROMPT" --model haiku 2>/dev/null)
# Clean up the response (remove any accidental markdown or extra whitespace)
GENERATED_CMD=$(echo "$GENERATED_CMD" | sed 's/^```[a-z]*//g' | sed 's/```$//g' | sed 's/^`//g' | sed 's/`$//g' | sed '/^$/d' | head -n1 | xargs)
if [ -z "$GENERATED_CMD" ]; then
echo -e "${RED}Error: Failed to generate command${NC}"
exit 1
fi
# Display the command
echo ""
echo -e "${GREEN}${BOLD}Command:${NC}"
echo -e "${YELLOW} $GENERATED_CMD${NC}"
echo ""
# Copy mode - just copy and exit
if [ "$COPY_MODE" = true ]; then
if copy_to_clipboard "$GENERATED_CMD"; then
echo -e "${GREEN}✓ Copied to clipboard${NC}"
else
echo -e "${RED}Could not copy to clipboard. Command:${NC}"
echo "$GENERATED_CMD"
fi
exit 0
fi
# Auto execute mode
if [ "$AUTO_EXECUTE" = true ]; then
echo -e "${BLUE}▶ Executing...${NC}"
echo "─────────────────────────────────────"
eval "$GENERATED_CMD"
EXIT_CODE=$?
echo "─────────────────────────────────────"
if [ $EXIT_CODE -eq 0 ]; then
echo -e "${GREEN}✓ Command completed successfully${NC}"
else
echo -e "${RED}✗ Command exited with code $EXIT_CODE${NC}"
fi
exit $EXIT_CODE
fi
# Interactive prompt
while true; do
echo -e "${CYAN}[e]xecute [a]bort [r]evise [?]explain [c]opy${NC}"
read -r -p "> " choice
case "$choice" in
e|E|execute)
echo ""
echo -e "${BLUE}▶ Executing...${NC}"
echo "─────────────────────────────────────"
eval "$GENERATED_CMD"
EXIT_CODE=$?
echo "─────────────────────────────────────"
if [ $EXIT_CODE -eq 0 ]; then
echo -e "${GREEN}✓ Command completed successfully${NC}"
else
echo -e "${RED}✗ Command exited with code $EXIT_CODE${NC}"
fi
break
;;
a|A|abort|q|Q|quit)
echo -e "${YELLOW}Aborted.${NC}"
break
;;
r|R|revise)
echo -e "${CYAN}How should I revise the command?${NC}"
read -r REVISION_REQUEST
if [ -n "$REVISION_REQUEST" ]; then
echo -e "${BLUE}⟳ Revising command...${NC}"
REVISE_PROMPT="You are a shell command generator. Revise the following shell command based on the user's request.
Current command: ${GENERATED_CMD}
Revision request: ${REVISION_REQUEST}
Rules:
1. Output ONLY the revised shell command, nothing else
2. No explanations, no markdown, no code blocks, no backticks
3. Keep the original intent but apply the requested changes"
NEW_CMD=$(claude -p "$REVISE_PROMPT" --model haiku 2>/dev/null)
NEW_CMD=$(echo "$NEW_CMD" | sed 's/^```[a-z]*//g' | sed 's/```$//g' | sed 's/^`//g' | sed 's/`$//g' | sed '/^$/d' | head -n1 | xargs)
if [ -n "$NEW_CMD" ]; then
GENERATED_CMD="$NEW_CMD"
fi
fi
echo ""
echo -e "${GREEN}${BOLD}Revised command:${NC}"
echo -e "${YELLOW} $GENERATED_CMD${NC}"
echo ""
;;
"?"|explain)
explain_command "$GENERATED_CMD"
;;
c|C|copy)
if copy_to_clipboard "$GENERATED_CMD"; then
echo -e "${GREEN}✓ Copied to clipboard${NC}"
else
echo -e "${RED}Could not copy to clipboard${NC}"
fi
;;
*)
echo -e "${RED}Invalid choice.${NC}"
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment