A utility for wrapping commands that need to change your shell's working directory.
Sometimes you have a command that needs to change your shell's working directory, but command substitution ($(...)
) and subshells can't modify the parent shell's working directory. This makes it impossible to write commands in languages like Rust that need to change your current directory.
cdwrap provides a way to wrap such commands so they can change your shell's working directory after they complete. It works by:
- Creating a temporary file for communication
- Running your command
- Reading any requested directory changes after the command completes
- Applying those changes to your shell
cargo install --path .
First, set up a wrapper for your command in your ~/.zshrc
or ~/.bashrc
:
# Replace 'yourcommand' with the name of the command you want to wrap
eval "$(cdwrap setup yourcommand)"
Now you can use your command as normal. If your command uses cdwrap cd
internally, it will change your shell's directory after the command completes.
Let's say you have a Rust CLI tool called dev
that needs to change directories. You would:
-
Add this to your shell config:
eval "$(cdwrap setup dev)"
-
In your Rust tool, when you need to change directories, use:
cdwrap cd /path/to/directory
Now when your tool runs cdwrap cd
, the shell wrapper will change to that directory after your tool exits.
Note that cdwrap cd
is a pretty minimal convenience: You can also just write the message directly.
Just write cd:<path>\n
to file descriptor 9.
The wrapper function:
- Creates a temporary file
- Opens it on file descriptors 8 (read) and 9 (write)
- Runs your command
- Reads any directory change requests from the temp file
- Applies the changes after your command exits
This allows your command to communicate directory changes back to the parent shell, which would otherwise be impossible.