encyclopegia.org

Command substitution

In computing, command substitution is a facility that allows a command to be run and its output to be pasted back on the command line as arguments to another command. Command substitution first appeared in the Bourne shell, introduced with Version 7 Unix in 1979, and has remained a characteristic of all later Unix shells. The feature has since been adopted in other programming languages as well, including Perl, PHP, Ruby and Microsoft's Powershell under Windows. It also appears in Microsoft's CMD.EXE in the FOR command and the ( ) command.

In computing, command substitution is a facility that allows a command to be run and its output to be pasted back on the command line as arguments to another command. Command substitution first appeared in the Bourne shell, introduced with Version 7 Unix in 1979, and has remained a characteristic of all later Unix shells. The feature has since been adopted in other programming languages as well, including Perl, PHP, Ruby and Microsoft's Powershell under Windows. It also appears in Microsoft's CMD.EXE in the FOR command and the ( ) command. == Syntax and semantics== Shells typically implement command substitution by creating a child process to run the first command with its standard output piped back to the shell, which reads that output, parsing it into words separated by whitespace. Because the shell can't know it has all the output from the child until the pipe closes or the child dies, it waits until then before it starts another child process to run the second command. This C shell example shows how one might search for all the C files containing the string malloc using fgrep and then edit any that are found using the vi editor. The syntactical notation shown here, `  ...  ` , using backquotes as delimiters, is the original style and is supported by all the common Unix shells. !/bin/csh vi `fgrep -l malloc *.c` Objections have been raised to both the syntax, how it's typed, and the semantics, how it works. While easy to type, an important factor for an interactive command processor, the syntax has been criticized as awkward to nest, putting one command substitution inside another, because both the left and the right delimiters are the same. The KornShell (ksh) solved this with an alternative notation, $(  ...  ) , borrowing from the notational style used for variable substitution. Today, most UNIX shells support this syntax. Microsoft's PowerShell also uses this notation, with the same semantics. !/bin/bash vi $(fgrep -l malloc *.c) The semantics, breaking the output into words at whitespace, has also been criticized. It worked well on early Unix systems where filenames never contained spaces but it doesn't work at all well on modern Windows and Linux systems where filenames certainly can contain spaces. In either of these previous examples, if any of the filenames matched by the *.c wildcard contains a space, that filename will be broken into two separate arguments to vi , clearly not what was intended. Hamilton C shell solved this with a double backquote notation, ``  ...  `` , that parses into words only at line breaks. This is an example of command substitution using the () operator in PowerShell: $MyVariable = (ls) echo $MyVariable ==Expression substitution== A related facility, expression substitution, is found in the languages Common Lisp and Scheme, invoked by using the comma-at operator in an expression marked with the backquote (or "quasiquote") operator, and in ABC, by using an expression enclosed between backquotes inside a text display (string literal). For example, the ABC command produces the output .

References

No reference rows published for this article.

Cite this article

MLA

"Command substitution." encyclopegia.org, 2026, https://encyclopegia.org/wiki/command-substitution. Accessed 24 November 2026.

Chicago

"Command substitution." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/command-substitution.

APA

encyclopegia.org. (2026). Command substitution. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/command-substitution

Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

unified combatant command

stub · references available

High Command of the Armed Forces

stub · references available

Sub Command

stub · references available

Hayes command set

stub · references available

substitution effect

stub · references available

command pattern

stub · references available

Air Mobility Command

stub · references available

Home Front Command

stub · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1361612481 · 0.5 · snapshot enwiki-001361612481

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.