what-the-path
A Rust library for detecting the current Unix shell, managing shell rc files, and manipulating your PATH.
Features
- Detect your active shell (Fish, Zsh, Bash, POSIX)
- Locate shell rc files (
.bashrc, .zshenv, conf.d for fish, etc.)
- Check if a directory is present in the
PATH environment variable
- Append or remove lines from shell rc files
Installation
Add to your Cargo.toml:
[dependencies]
what-the-path = "0.1"
Usage
use what_the_path::{Shell, exists_in_path, append_to_rcfile, remove_from_rcfile};
use std::path::PathBuf;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let shell = Shell::detect_by_shell_var()?;
let rcfiles = shell.get_rcfiles()?;
println!("Detected shell: {:?}\nRC files: {:?}", shell, rcfiles);
let dir = "/usr/local/bin";
if exists_in_path(dir) {
println!("{} is in PATH", dir);
}
let rc = PathBuf::from("/home/user/.bashrc");
append_to_rcfile(rc.clone(), "export FOO=bar")?;
remove_from_rcfile(rc, "export FOO=bar")?;
Ok(())
}