escapeshellarg
escapeshellarg(string $arg): string
Escapes a string into a safe single shell argument (against command injection).
C C23
#include <unistd.h>
int main(void) {
/* no stdlib escaper: the safe path skips the shell and hands argv to exec directly */
execlp("echo", "echo", "O'Brien; rm -rf /", (char *)NULL);
return 0; /* reached only if exec fails */
}
O'Brien; rm -rf /
C++ C++26
#include <unistd.h>
int main() {
// no stdlib escaper: the safe path skips the shell and hands argv to exec directly
execlp("echo", "echo", "O'Brien; rm -rf /", (char *)NULL);
return 0;
}
O'Brien; rm -rf /
C# v11.0.100
using System.Diagnostics;
// ArgumentList passes the string verbatim: no shell parsing, no escaping needed
var psi = new ProcessStartInfo("echo");
psi.ArgumentList.Add("O'Brien; rm -rf /");
psi.RedirectStandardOutput = true;
var p = Process.Start(psi)!;
Console.WriteLine(p.StandardOutput.ReadToEnd().Trim());
p.WaitForExit();
O'Brien; rm -rf /
Go v1.27.4
package main
import (
"fmt"
"os/exec"
"strings"
)
func main() {
// exec.Command passes the argument verbatim: no shell, no escaping needed
out, _ := exec.Command("echo", "O'Brien; rm -rf /").Output()
fmt.Println(strings.TrimSpace(string(out)))
}
O'Brien; rm -rf /
Java v27.0.1
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) throws Exception {
// ProcessBuilder passes the argument verbatim: no shell, no escaping needed
Process p = new ProcessBuilder("echo", "O'Brien; rm -rf /").start();
String out = new String(p.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
System.out.println(out.trim());
p.waitFor();
}
}
O'Brien; rm -rf /
JavaScript v26.13.1
const { execFileSync } = require("node:child_process");
// no built-in escapeshellarg: the safe path is execFileSync's argument array
const out = execFileSync("echo", ["O'Brien; rm -rf /"]);
console.log(out.toString().trim());
O'Brien; rm -rf /
PHP v8.6.0
$raw = "O'Brien; rm -rf /";
echo escapeshellarg($raw), PHP_EOL; // wrapped in '...', inner quotes escaped: ONE inert argument
echo trim(shell_exec('echo ' . escapeshellarg($raw))), PHP_EOL; // the ; never reaches the shell
'O'\''Brien; rm -rf /' O'Brien; rm -rf /
Python v3.15.2
import shlex
import subprocess
raw = "O'Brien; rm -rf /"
print(shlex.quote(raw)) # POSIX quoting, same intent as escapeshellarg
r = subprocess.run(["echo", raw], capture_output=True, text=True) # argv: no quoting needed at all
print(r.stdout.strip())
'O'"'"'Brien; rm -rf /' O'Brien; rm -rf /
Rust v1.99.1
use std::process::Command;
fn main() {
// .arg() passes the string verbatim: no shell, no escaping needed
let out = Command::new("echo").arg("O'Brien; rm -rf /").output().unwrap();
println!("{}", String::from_utf8_lossy(&out.stdout).trim());
}
O'Brien; rm -rf /
TypeScript v26.13.1
import { execFileSync } from "node:child_process";
// no built-in escapeshellarg: the safe path is execFileSync's argument array
const out: string = execFileSync("echo", ["O'Brien; rm -rf /"]).toString();
console.log(out.trim());
O'Brien; rm -rf /
Language differences
C: the standard library has no shell escaper — the right move is not to invent escaping but to bypass the shell: execvp/execlp pass a per-argument array (example), so a malicious string stays pure data. Hand-rolling single-quote escaping for a shell string is notoriously error-prone.
C++: same as C (example); the C++ standard library likewise has no escaper, and passing a string to std::system carries the same danger as C's system.
C#: ArgumentList adds one argument at a time (example) and the process runs without a shell — no escaping needed; the ProcessStartInfo.Arguments string is the injection surface, and the docs explicitly recommend against it.
Go: exec.Command with an argument array is safe (example); the only path to a shell is an explicit sh -c, and Go's standard library has no escapeshellarg equivalent — redesign instead.
Java: ProcessBuilder with an argument array is safe (example); Java has never shipped a shell-escaping tool — the design itself steers you away from shells.
JavaScript: Node ships no escapeshellarg; the safe pattern is execFileSync/spawnSync with an argument array (example). When a shell is truly required, the community shell-escape library does the quoting — hand-writing it is a leak waiting to happen.
PHP: escapeshellarg wraps the argument as a single-quoted literal (on Unix inner ' becomes '\'', first line of the example); Windows uses double-quote rules — quoting shapes differ across platforms but the safety goal is identical; unrelated to addslashes, don't mix them up.
Python: shlex.quote is the only proper escaper besides argument arrays (first line of the example); its style differs from PHP's (close-and-reopen '"'"') but the semantics match. Argument arrays (subprocess.run with a list) don't need it at all — the second line is the preferred approach.
Rust: Command with an argument array is safe (example); no escaper exists in the standard library — shells require an explicit sh -c and its injection surface, so redesign instead.
TypeScript: same as JavaScript; the one-line principle — escaping is the fallback for "must build a shell string", argument arrays are the answer, pick one.