A Bash script for running llm with templates, prompts, and models. It’s flexible. You can start a new chat, continue an old one, or use a template. It works with plain prompts or lets you write in your editor.
If you use -c, it continues the last chat. You can give a prompt or write one in your editor.
If you use -m model, it picks that model for llm.
If you give one word (like linuxcmd), it treats it as a template and asks for your prompt in the editor.
If you give a sentence, it treats it as a prompt and sends it to llm.
If you give no arguments, it opens your editor for a prompt and starts a new chat.
If you give two arguments, the first is the template, the second is the prompt.
| Command | What it does |
|---|---|
| amsh_llm_chat.sh -c -m gpt-4.1 "your prompt" | Continues previous chat with model gpt-4.1, sends prompt |
| amsh_llm_chat.sh linux "your prompt" | Uses template linux, sends prompt |
| amsh_llm_chat.sh linuxcmd | Uses template linuxcmd, opens editor for prompt |
| amsh_llm_chat.sh "Write a story about a cat" | Sends prompt directly, starts new chat |
| amsh_llm_chat.sh -c -m gpt-4.1 | Continues previous chat with model gpt-4.1, opens editor for prompt |
| amsh_llm_chat.sh -m gpt-4.1 | Starts new chat with model gpt-4.1, opens editor for prompt |
You need these tools installed:
A text editor set in $EDITOR (like vim, nano, or code)
Works with Bash on Linux.
#!/usr/bin/env bash
# amsh_llm_chat.sh
#
# Launch llm chat with flexible argument parsing for template, prompt, and model.
#
# USAGE:
# amsh_llm_chat.sh [-c] [-m model] [prompt]
# amsh_llm_chat.sh <template> [prompt]
#
# ARGUMENTS:
# -c Use llm -c (template ignored). Prompt can be provided or entered via editor.
# -m model Specify model to use with llm (-m <model>)
# <template> Template name for llm (single word, if only one argument and no -c)
# [prompt] Prompt to send to llm (if only one argument and contains spaces, template is ignored)
#
# BEHAVIOR:
# - If -c is provided, template is ignored. Prompt can be provided or entered via editor.
# - If only one non-switch argument:
# - If it is a single word (A-Za-z_-), it is treated as template. Editor opens for prompt.
# - If it contains spaces, it is treated as prompt and sent directly to llm.
# - If no arguments, opens $EDITOR for prompt and sends to llm (starts new chat).
# - If two or more arguments, first is template, second is prompt (llm -t <template>).
#
# EXAMPLES:
# amsh_llm_chat.sh -c -m gpt-4.1 "your prompt"
# amsh_llm_chat.sh linux "your prompt"
# amsh_llm_chat.sh grammar
# amsh_llm_chat.sh "Write a story about a cat"
# amsh_llm_chat.sh -c -m gpt-4.1
# amsh_llm_chat.sh -m gpt-4.1
#
set -euo pipefail
show_help() {
echo "Usage: $0 [-c] [-m model] [prompt] | <template> [prompt]"
echo " -c Use llm -c (template ignored) to continue with previous chat."
echo " Prompt can be provided or entered via editor."
echo " -m model Specify model to use with llm (-m <model>)"
echo " [prompt] Prompt to send to llm (if only one argument, template is ignored)"
echo " <template> Template name for llm (required if two arguments and no -c)"
echo "If no arguments, opens $EDITOR for prompt and sends to llm without template."
}
# Argument parsing for -c and -m flags
USE_C=false
MODEL=""
ARGS=()
while [[ $# -gt 0 ]]; do
case "$1" in
-c)
USE_C=true
shift
;;
-m)
MODEL="$2"
shift 2
;;
*)
ARGS+=("$1")
shift
;;
esac
done
if [[ "${ARGS[0]-}" == "-h" || "${ARGS[0]-}" == "--help" ]]; then
show_help
exit 0
fi
LLM_MODEL_ARG=""
if [[ -n "$MODEL" ]]; then
LLM_MODEL_ARG="-m $MODEL"
fi
if $USE_C; then
if [[ ${#ARGS[@]} -ge 1 ]]; then
PROMPT="${ARGS[0]}"
llm -c "$PROMPT" $LLM_MODEL_ARG | sd
else
TMPFILE="$(mktemp /tmp/llm_prompt.XXXXXX)"
trap 'rm -f "$TMPFILE"' EXIT
${EDITOR:-vi} "$TMPFILE"
if [[ ! -s "$TMPFILE" ]]; then
echo "Prompt is empty. Aborting."
exit 2
fi
PROMPT="$(cat "$TMPFILE")"
llm -c "$PROMPT" $LLM_MODEL_ARG | sd
fi
elif [[ ${#ARGS[@]} -eq 1 ]]; then
ARG1="${ARGS[0]}"
if [[ "$ARG1" =~ ^[A-Za-z_-]+$ ]]; then
# Single word: treat as template, open editor for prompt
TEMPLATE="$ARG1"
TMPFILE="$(mktemp /tmp/llm_prompt.XXXXXX)"
trap 'rm -f "$TMPFILE"' EXIT
${EDITOR:-vi} "$TMPFILE"
if [[ ! -s "$TMPFILE" ]]; then
echo "Prompt is empty. Aborting."
exit 2
fi
cat "$TMPFILE" | llm -t "$TEMPLATE" $LLM_MODEL_ARG | sd
rm -f "$TMPFILE"
else
# Multiple words: treat as prompt
PROMPT="$ARG1"
llm "$PROMPT" $LLM_MODEL_ARG | sd
fi
elif [[ ${#ARGS[@]} -eq 0 ]]; then
TMPFILE="$(mktemp /tmp/llm_prompt.XXXXXX)"
trap 'rm -f "$TMPFILE"' EXIT
${EDITOR:-vi} "$TMPFILE"
if [[ ! -s "$TMPFILE" ]]; then
echo "Prompt is empty. Aborting."
exit 2
fi
cat "$TMPFILE" | llm $LLM_MODEL_ARG | sd
rm -f "$TMPFILE"
elif [[ ${#ARGS[@]} -ge 2 ]]; then
TEMPLATE="${ARGS[0]}"
PROMPT="${ARGS[1]}"
echo "$PROMPT" | llm -t "$TEMPLATE" $LLM_MODEL_ARG | sd
else
show_help
exit 1
fi