Debian Codegraph @

README.md

raw symbols SCIP
1 <!--
2 References; overridden in `src/lib.rs`.
3 See https://linebender.org/blog/doc-include/.
4 -->
5
6 [`&str`]: https://doc.rust-lang.org/stable/std/primitive.str.html
7 [`String`]: https://doc.rust-lang.org/stable/alloc/string/struct.String.html
8 [`bstr::BStr`]: https://docs.rs/bstr/latest/bstr/struct.BStr.html
9 [`bstr::BString`]: https://docs.rs/bstr/latest/bstr/struct.BString.html
10 [`slice`]: https://doc.rust-lang.org/stable/std/primitive.slice.html
11 [`Vec<u8>`]: https://doc.rust-lang.org/stable/std/vec/struct.Vec.html
12 [`OsStr`]: https://doc.rust-lang.org/stable/std/ffi/struct.OsStr.html
13 [`OsString`]: https://doc.rust-lang.org/stable/std/ffi/struct.OsString.html
14 [`Path`]: https://doc.rust-lang.org/stable/std/path/struct.Path.html
15 [`PathBuf`]: https://doc.rust-lang.org/stable/std/path/struct.PathBuf.html
16 [`Sh`]: https://docs.rs/shell-quote/latest/shell_quote/struct.Sh.html
17 [`Dash`]: https://docs.rs/shell-quote/latest/shell_quote/struct.Dash.html
18 [`Bash`]: https://docs.rs/shell-quote/latest/shell_quote/struct.Bash.html
19 [`Fish`]: https://docs.rs/shell-quote/latest/shell_quote/struct.Fish.html
20 [`Zsh`]: https://docs.rs/shell-quote/latest/shell_quote/struct.Zsh.html
21 [`QuoteRefExt`]: https://docs.rs/shell-quote/latest/shell_quote/trait.QuoteRefExt.html
22 [`QuoteRefExt::quoted`]: https://docs.rs/shell-quote/latest/shell_quote/trait.QuoteRefExt.html#tymethod.quoted
23 [`QuoteExt`]: https://docs.rs/shell-quote/latest/shell_quote/trait.QuoteExt.html
24
25 <!-- References end. -->
26
27 <div class="readme-only">
28
29 # shell-quote
30
31 </div>
32
33 **shell-quote** escapes strings in a way that they can be inserted into shell
34 scripts without the risk that they're interpreted as, say, multiple arguments
35 (like with Bash's _word splitting_), paths (Bash's _pathname expansion_), shell
36 metacharacters, function calls, or other syntax. This is frequently not as
37 simple as wrapping a string in quotes.
38
39 This package implements escaping for [GNU Bash][gnu-bash], [Z Shell][z-shell],
40 [fish][], and `/bin/sh`-like shells including [Dash][dash].
41
42 [dash]: https://en.wikipedia.org/wiki/Almquist_shell#dash
43 [gnu-bash]: https://www.gnu.org/software/bash/
44 [z-shell]: https://zsh.sourceforge.io/
45 [fish]: https://fishshell.com/
46
47 It can take as input many different string and byte string types:
48
49 - [`&str`] and [`String`]
50 - [`&bstr::BStr`][`bstr::BStr`] and [`bstr::BString`]
51 - [`&[u8]`][`slice`] and [`Vec<u8>`]
52 - [`&OsStr`][`OsStr`] and [`OsString`] (on UNIX)
53 - [`&Path`][`Path`] and [`PathBuf`]
54
55 and produce output as (or push into) the following types:
56
57 - [`String`] (for shells that support it, i.e. not [`Sh`]/[`Dash`])
58 - [`bstr::BString`]
59 - [`Vec<u8>`]
60 - [`OsString`] (on UNIX)
61
62 Inspired by the Haskell [shell-escape][] package.
63
64 [shell-escape]: https://github.com/solidsnack/shell-escape
65
66 ## Examples
67
68 When quoting using raw bytes it can be convenient to call [`Sh`]'s, [`Dash`]'s,
69 [`Bash`]'s, [`Fish`]'s, and [`Zsh`]'s associated functions directly:
70
71 ```rust
72 use shell_quote::{Bash, Dash, Fish, Sh, Zsh};
73 // No quoting is necessary for simple strings.
74 assert_eq!(Sh::quote_vec("foobar"), b"foobar");
75 assert_eq!(Dash::quote_vec("foobar"), b"foobar"); // `Dash` is an alias for `Sh`
76 assert_eq!(Bash::quote_vec("foobar"), b"foobar");
77 assert_eq!(Zsh::quote_vec("foobar"), b"foobar"); // `Zsh` is an alias for `Bash`
78 assert_eq!(Fish::quote_vec("foobar"), b"foobar");
79 // In all shells, quoting is necessary for strings with spaces.
80 assert_eq!(Sh::quote_vec("foo bar"), b"foo' bar'");
81 assert_eq!(Dash::quote_vec("foo bar"), b"foo' bar'");
82 assert_eq!(Bash::quote_vec("foo bar"), b"$'foo bar'");
83 assert_eq!(Zsh::quote_vec("foo bar"), b"$'foo bar'");
84 assert_eq!(Fish::quote_vec("foo bar"), b"foo' bar'");
85 ```
86
87 It's also possible to use the extension trait [`QuoteRefExt`] which provides a
88 [`quoted`][`QuoteRefExt::quoted`] function:
89
90 ```rust
91 use shell_quote::{Bash, Sh, Fish, QuoteRefExt};
92 let quoted: String = "foo bar".quoted(Bash);
93 assert_eq!(quoted, "$'foo bar'");
94 let quoted: Vec<u8> = "foo bar".quoted(Sh);
95 assert_eq!(quoted, b"foo' bar'");
96 let quoted: String = "foo bar".quoted(Fish);
97 assert_eq!(quoted, "foo' bar'");
98 ```
99
100 Or the extension trait [`QuoteExt`] for pushing quoted strings into a buffer:
101
102 ```rust
103 use shell_quote::{Bash, QuoteExt};
104 let mut script: bstr::BString = "echo ".into();
105 script.push_quoted(Bash, "foo bar");
106 script.extend(b" > ");
107 script.push_quoted(Bash, "/path/(to)/[output]");
108 assert_eq!(script, "echo $'foo bar' > $'/path/(to)/[output]'");
109 ```
110
111 ## Notes on string encoding
112
113 <div class="warning">
114
115 Here we will use [`Bash`] for the example, but other shells may have similar _or
116 different_ behaviours; check their documentation.
117
118 </div>
119
120 When we use [`&str`] or [`String`] as an input type, UTF-8 code points of U+0080
121 and above are written into the quoted form just as they are encoded in UTF-8,
122 i.e. the bytes are the same and there are no escape sequences. Compare this to
123 using a different input type:
124
125 ```rust
126 # use shell_quote::{Bash, QuoteRefExt};
127 let data: &str = "café";
128 let data_utf8_quoted_from_string_type: Vec<u8> = data.quoted(Bash);
129 assert_eq!(&data_utf8_quoted_from_string_type, b"$'caf\xC3\xA9'"); // UTF-8, verbatim.
130 let data_utf8_quoted_from_bytes: Vec<u8> = data.as_bytes().quoted(Bash);
131 assert_eq!(&data_utf8_quoted_from_bytes, b"$'caf\\xC3\\xA9'"); // Now hex escaped!
132 ```
133
134 It follows then, supposing you need to use a text encoding that is not UTF-8,
135 that string types must be encoded _before_ passing to the functions from this
136 crate.
137
138 For example, the character 'é' (U+00E9):
139
140 - In ISO-8859-1, it is represented by the single byte `0xE9`.
141 - In UTF-8, it is represented by the two bytes `0xC3 0xA9`.
142
143 Using a hypothetical `encode_iso_8859_1` function:
144
145 ```rust
146 # use shell_quote::{Bash, QuoteRefExt};
147 # fn encode_iso_8859_1(_s: &str) -> &[u8] {
148 # &[99, 97, 102, 233]
149 # }
150 let data = "café";
151 let data_utf8_quoted: Vec<u8> = data.quoted(Bash);
152 assert_eq!(&data_utf8_quoted, b"$'caf\xC3\xA9'"); // UTF-8: 2 bytes for é.
153 let data_iso_8859_1: &[u8] = encode_iso_8859_1(data);
154 let data_iso_8859_1_quoted: Vec<u8> = data_iso_8859_1.quoted(Bash);
155 assert_eq!(&data_iso_8859_1_quoted, b"$'caf\\xE9'"); // ISO-8859-1: 1 byte, hex escaped.
156 ```
157
158 ## Compatibility
159
160 [`Sh`] can serve as a lowest common denominator for Bash, Z Shell, and
161 `/bin/sh`-like shells like Dash. However, fish's quoting rules are different
162 enough that you must use [`Fish`] for fish scripts.
163
164 Note that using [`Sh`] as a lowest common denominator brings with it other
165 issues; read its documentation carefully to understand the limitations.
166
167 ## Feature flags
168
169 The following are all enabled by default:
170
171 - `bstr`: Support [`bstr::BStr`] and [`bstr::BString`].
172 - `bash`: Support [Bash][gnu-bash] and [Z Shell][z-shell].
173 - `fish`: Support [fish][].
174 - `sh`: Support `/bin/sh`-like shells including [Dash][dash].
175
176 To limit support to specific shells, you must disable this crate's default
177 features in `Cargo.toml` and re-enable those you want. For example:
178
179 ```toml
180 [dependencies]
181 shell-quote = { version = "*", default-features = false, features = ["bash"] }
182 ```