Skip to content
API Documentation
BastionGuard™
shell_quote.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4
6
7// Quote a single argument for POSIX-compatible shells. In particular, a
8// single quote inside the argument must not terminate the quoted string.
9// Prefer bg_process::run(argv) where a shell is not actually required.
10inline std::string shell_quote(const std::string& argument) {
11 std::string quoted;
12 quoted.reserve(argument.size() + 2);
13 quoted.push_back('\'');
14 for (const char ch : argument) {
15 if (ch == '\'') quoted += "'\"'\"'";
16 else quoted.push_back(ch);
17 }
18 quoted.push_back('\'');
19 return quoted;
20}
21
22} // namespace BastionGuard::wizard
std::string shell_quote(const std::string &argument)