| Crates.io | quotemeta |
| lib.rs | quotemeta |
| version | 0.1.2 |
| created_at | 2020-01-12 07:07:30.02599+00 |
| updated_at | 2023-06-02 11:57:57.313737+00 |
| description | Shell-quoting, à la Perl's `quotemeta` function. |
| homepage | |
| repository | https://github.com/mooli/quotemeta-rs |
| max_upload_size | |
| id | 197719 |
| size | 19,432 |
Shell-quoting, à la Perl's quotemeta function.
One idiom when writing simple shell tools in Rust (or Perl or whatever) is to spit out a shell
script or shell fragment for later perusal and/or piping directly into a shell. However, a
simple println! generates an incorrect and potentially insecure script if the filename happens
to contain shell metacharacters. This includes the not exactly uncommon space character. Perl
includes a quotemeta function which usually does the job. Let's do the job properly!
(Actually, you can't even println! a plain Path/PathBuf because they don't implement
Display.)
This crate provides a function which quotes and escapes filenames (or other data) such that it can be interpolated. For example:
use quotemeta::quotemeta;
fn main() {
for path in std::env::args_os().skip(1) {
println!("cat {}", quotemeta(path));
}
}
This will work even if the filename contains a carriage return, or is invalid UTF-8.