Shell Integration
How enx integrates with your shell to enable features like enx cd.
Table of Contents
- TOC
Why Shell Integration?
A child process cannot change the parent shell’s working directory. This means enx cd can’t directly cd you into a project — it needs a shell wrapper function that captures the output and calls the shell’s built-in cd.
enx setup regenerates shell integration for both bash and zsh automatically. If you need to set it up manually, see the snippets below.
Automatic Setup
When you run enx setup, it regenerates enx shell scripts and appends/repairs source lines in your shell config files:
| Shell | Config file |
|---|---|
| Bash | ~/.bashrc |
| Zsh | ~/.zshrc |
| Git Bash (Windows) | ~/.bashrc |
If shell integration doesn’t take effect immediately, restart your shell or source the config file (e.g.
source ~/.bashrc).
Manual Setup
Bash / Zsh
Add this to your ~/.bashrc or ~/.zshrc:
enx() {
if [ "$1" = "cd" ]; then
shift
local dir
dir=$(command enx cd "$@")
if [ $? -eq 0 ]; then
cd "$dir" || return 1
else
return 1
fi
else
command enx "$@"
fi
}
How It Works
The wrapper function intercepts calls to enx:
- If the first argument is
cd, it runscommand enx cd <args>— the actual binary - The binary prints the matched project’s path to stdout
- The wrapper captures that path and calls the shell’s built-in
cd - For all other subcommands, the wrapper passes through to the binary directly
This means enx cd works transparently — you type enx cd api and your shell changes directory, even though the enx binary itself can’t do that.
All other enx commands work without shell integration. Only
enx cdrequires the wrapper function.
Verifying Integration
After setup, test that it works:
# Register a project if you haven't already
enx init
# Navigate away and back
cd /tmp
enx cd my-project
pwd # should show your project's path
If pwd shows the correct path, shell integration is working.
Troubleshooting
If bash starts with errors like autoload: command not found or compdef: command not found, a zsh completion script is being sourced in bash.
Fix steps:
- Run
command enx setupin bash. - Restart bash.
- If errors persist, remove old enx source lines from
~/.bashrc, then runcommand enx setupagain.