rust_cmd_lib is a Rust library designed to make it easier to write shell-script–style tasks in Rust, blending the power and safety of Rust with the expressiveness of shell pipelines. It provides macros and utilities that let you spawn external processes, redirect input/output, and pipe commands together, all without invoking a shell. You can write something like run_cmd!(ls -l | grep foo > out.txt) in a more declarative style, rather than manually wiring up file descriptors, handles, and child processes. The library also supports features like variable substitution, scoped environment settings, and defining custom commands (functions) that behave like built-ins. It hides much of the boilerplate of std::process::Command when you're doing simple task automations, but still allows full flexibility when needed. Because it avoids launching a shell, it reduces some classes of security and quoting errors, while improving readability of scripting logic inside a Rust binary.
Features
- Macros like run_cmd! and run_fun! to execute external commands with capturing output, chaining, error handling etc.
- Support for piping and redirection (so you can write pipes and redirects inside macro usage, not manually spawn subprocesses and wire IO)
- Utilities for passing parameters more intuitively (literal strings, variable interpolation) so scripts feel more natural than verbose process spawning code
- Feature flags and optional dependencies so you can enable or disable parts of functionality depending on your needs (logging, pipefail, etc.)
- Examples and test suite, including “tetris game example” among others, showing how more complex shell script behavior can be expressed.
- A library design: no spawning of extra shell when possible, so reduced runtime overhead and more safety around escaping, etc.