shell-integration

Original🇺🇸 English
Translated

Shell scripting and terminal integration patterns for building tools that integrate with Zsh, Bash, and Fish. Covers completion systems (compdef/compadd, complete/compgen, fish complete), ZLE widgets, hooks (precmd/preexec/chpwd, PROMPT_COMMAND), readline, bindkey, parameter expansion, ZDOTDIR loading order, event systems, abbreviations, POSIX shell scripting, terminal control codes (ANSI/CSI escape sequences), tput, stty, signal handling, process management (job control, traps), and shell plugin distribution patterns. Use when building shell plugins, writing completion scripts, implementing terminal UI with escape sequences, managing dotfiles, creating installation scripts, handling signals and process management, or integrating native binaries with shell wrappers.

6installs
Added on

NPX Install

npx skill4agent add oakoss/agent-skills shell-integration

Shell Integration

Overview

Shell integration covers the APIs and patterns for building tools that extend or interact with Unix shells. This includes completion systems, prompt hooks, key bindings, terminal control, and plugin distribution across Zsh, Bash, and Fish.
When to use: Building CLI tool completions, shell plugins, prompt customizations, terminal UI, dotfile managers, installation scripts, or native binary wrappers.
When NOT to use: General-purpose scripting unrelated to shell extension (use POSIX scripting reference for standalone scripts), GUI applications, or web server development.

Quick Reference

PatternShellKey Points
Completion functionZsh
compdef
,
compadd
,
zstyle
for matcher configuration
Completion functionBash
complete
,
compgen
,
COMP_WORDS
,
COMP_CWORD
,
COMPREPLY
Completion functionFish
complete -c cmd -a args
, condition flags, subcommand patterns
ZLE widgetZsh
zle -N widget func
,
bindkey
to map keys
Prompt hookZsh
precmd
,
preexec
,
chpwd
via
add-zsh-hook
Prompt hookBash
PROMPT_COMMAND
(string or array in Bash 5.1+)
Event handlerFish
--on-event
,
--on-variable
,
--on-signal
AbbreviationFish
abbr -a name expansion
,
--function
for dynamic
Parameter expansionZsh
${(s.:.)var}
,
${var:=default}
, flags and modifiers
Terminal controlAllANSI/CSI escape sequences,
tput
,
stty
Signal handlingAll
trap
builtin, cleanup patterns,
EXIT
/
INT
/
TERM
Process managementAllJob control (
&
,
wait
,
bg
,
fg
), subshells, coprocesses
Plugin installationAllSourcing strategies, version detection,
ZDOTDIR
loading order

Common Mistakes

MistakeCorrect Pattern
Using
echo -e
for escape sequences portably
Use
printf
or
tput
for portability across shells and OSes
Modifying
PROMPT_COMMAND
with
=
in Bash
Append with
+=
to avoid overwriting other tools
Defining Fish event handlers in lazy-loaded functionsPlace event handlers in config.fish or source them explicitly
Hardcoding terminal capabilitiesQuery via
tput
which respects
TERM
and terminfo
Missing
emulate -L zsh
in Zsh functions
Always set local options to avoid polluting caller environment
Using
$COMP_LINE
splitting instead of
COMP_WORDS
Use
COMP_WORDS[$COMP_CWORD]
for reliable word extraction
Not quoting
$@
in wrapper scripts
Always use
"$@"
to preserve argument boundaries
Assuming
/bin/sh
is Bash
Target POSIX sh for portable scripts, test with
dash
Using
function
keyword in POSIX scripts
Use
name() { ... }
syntax for POSIX compatibility
Ignoring
EXIT
trap for cleanup
Always set
trap cleanup EXIT
for temp files and state

Delegation

  • Completion testing: Use
    Explore
    agent to verify completions interactively
  • Script review: Use
    Task
    agent for cross-shell compatibility audits
  • Code review: Delegate to
    code-reviewer
    agent
If the
rust
skill is available, delegate native binary compilation patterns to it. Shell wrappers often invoke Rust-compiled binaries for performance-critical operations. If the
cli-power-tools
skill is available, delegate modern CLI utility patterns to it. Many shell plugins wrap tools like
fd
,
ripgrep
, and
fzf
.

References

  • Zsh integration: ZLE, completions, hooks, parameter expansion
  • Bash integration: readline, completions, PROMPT_COMMAND, shopt
  • Fish integration: completions, events, abbreviations, functions
  • Terminal control: ANSI/CSI sequences, tput, stty, capabilities
  • POSIX scripting: portable patterns, signal handling, process management
  • Plugin distribution: installation scripts, dotfile management, version detection