Skip to main content

agentmux_common/
shell_quote.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! POSIX shell quoting, shared by srv and the CEF host.
5//! (bashwrap's `hook.rs` has its own platform-aware variant that also leaves
6//! safe strings bare; it is deliberately different.)
7
8/// Wrap `s` in single quotes for bash/sh/zsh, escaping each `'` as `'\''`.
9/// Always quotes, so the result is one word whatever `s` contains.
10pub fn posix_single_quote(s: &str) -> String {
11    format!("'{}'", s.replace('\'', "'\\''"))
12}
13
14#[cfg(test)]
15mod tests {
16    use super::*;
17
18    #[test]
19    fn quotes_every_input_as_one_word() {
20        assert_eq!(posix_single_quote("plain"), "'plain'");
21        assert_eq!(posix_single_quote(""), "''");
22        assert_eq!(posix_single_quote("a b $HOME `x`"), "'a b $HOME `x`'");
23        assert_eq!(posix_single_quote("it's"), "'it'\\''s'");
24    }
25}