shell_exec
shell_exec(string $command): string|false|null
Executes a command via the shell and returns the full output as a string (equivalent to the backtick syntax).
C C23
#include <stdio.h>
#include <string.h>
int main(void) {
/* read the whole stream into one buffer: shell_exec returns all output */
FILE *fp = popen("echo hello shell_exec", "r");
char all[256] = "";
char buf[64];
while (fgets(buf, sizeof buf, fp)) strcat(all, buf);
pclose(fp);
all[strcspn(all, "\n")] = '\0'; /* strip the trailing newline */
printf("%s\n", all);
return 0;
}
hello shell_exec
C++ C++26
#include <cstdio>
#include <iostream>
#include <string>
int main() {
// accumulate the whole stream into one string, like shell_exec
FILE *fp = popen("echo hello shell_exec", "r");
std::string all;
char buf[64];
while (fgets(buf, sizeof buf, fp)) all += buf;
pclose(fp);
while (!all.empty() && (all.back() == '\n' || all.back() == '\r')) all.pop_back();
std::cout << all << "\n";
return 0;
}
hello shell_exec
C# v11.0.100
using System.Diagnostics;
var psi = new ProcessStartInfo("echo");
psi.ArgumentList.Add("hello");
psi.ArgumentList.Add("shell_exec");
psi.RedirectStandardOutput = true;
var p = Process.Start(psi)!;
// ReadToEnd captures the full stdout, like shell_exec
Console.WriteLine(p.StandardOutput.ReadToEnd().Trim());
p.WaitForExit();
hello shell_exec
Go v1.27.4
package main
import (
"fmt"
"os/exec"
"strings"
)
func main() {
// Output() buffers the whole stdout as one slice, like shell_exec
out, _ := exec.Command("echo", "hello", "shell_exec").Output()
fmt.Println(strings.TrimSpace(string(out)))
}
hello shell_exec
Java v27.0.1
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) throws Exception {
// readAllBytes buffers the whole stdout, like shell_exec's return value
Process p = new ProcessBuilder("echo", "hello", "shell_exec").start();
String out = new String(p.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
System.out.println(out.trim());
p.waitFor();
}
}
hello shell_exec
JavaScript v26.13.1
const { execSync } = require("node:child_process");
// execSync buffers the whole stdout as a Buffer and returns it, like shell_exec
const out = execSync("echo hello shell_exec");
console.log(out.toString().trim());
hello shell_exec
PHP v8.6.0
// shell_exec returns the FULL stdout (the backtick syntax is an alias, deprecated since PHP 8.5), null on failure
$out = shell_exec('echo hello shell_exec');
echo trim($out), PHP_EOL;
hello shell_exec
Python v3.15.2
import subprocess
# check_output captures the full stdout and returns it, like shell_exec
out = subprocess.check_output(["echo", "hello", "shell_exec"], text=True)
print(out.strip())
hello shell_exec
Rust v1.99.1
use std::process::Command;
fn main() {
// .output() buffers the whole stdout, like shell_exec
let out = Command::new("echo").arg("hello").arg("shell_exec").output().unwrap();
println!("{}", String::from_utf8_lossy(&out.stdout).trim());
}
hello shell_exec
TypeScript v26.13.1
import { execSync } from "node:child_process";
// execSync buffers the whole stdout and returns it, like shell_exec
const out: string = execSync("echo hello shell_exec").toString();
console.log(out.trim());
hello shell_exec
Language differences
C: no standard API returns the whole output — popen reads block by block into one string (example); the only failure signal is pclose's return value, so telling empty output from failure is up to you.
C++: the same popen + string accumulation as C (example); C++17 still has no process API — full pipe abstractions arrive only with third-party boost::process.
C#: RedirectStandardOutput + ReadToEnd is exactly "grab everything at once" (example); note ReadToEnd can deadlock when both stdout and stderr need reading — read one of them asynchronously.
Go: Output() buffers stdout into one byte slice (example) and pipes stderr to the parent; use CombinedOutput() to grab everything including the error stream — closer to shell_exec's behavior.
Java: readAllBytes reads the whole stdout in one go (example); getErrorStream reads stderr separately — when capturing both, read them independently to avoid a full pipe blocking.
JavaScript: execSync returns the full stdout (example, shell-based like PHP); timeouts and non-zero exits throw — the partial output lives in e.stdout inside the try/catch.
PHP: shell_exec is synonymous with the backtick syntax $out = \cmd\ (backticks deprecated since PHP 8.5) and returns the full output string; failure returns null (false for some errors since PHP 7) — the example trims the command's own trailing newline.
Python: check_output captures and returns the full stdout (example); a non-zero exit raises CalledProcessError (e.output carries the partial output); add stderr=subprocess.STDOUT to fold stderr into the result.
Rust: .output() returns an Output struct with stdout/stderr as separate fields and status standalone (example) — more structured than shell_exec; failure isn't an error but status.success().
TypeScript: same as JavaScript; execSync's return type depends on the encoding option — { encoding: "buffer" } gets you the raw Buffer.