In
computing
Computing is any goal-oriented activity requiring, benefiting from, or creating computer, computing machinery. It includes the study and experimentation of algorithmic processes, and the development of both computer hardware, hardware and softw ...
, command substitution is a facility that allows a
command
Command may refer to:
Computing
* Command (computing), a statement in a computer language
* command (Unix), a Unix command
* COMMAND.COM, the default operating system shell and command-line interpreter for DOS
* Command key, a modifier key on A ...
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
The Bourne shell (sh) is a shell command-line interpreter for computer operating systems. It first appeared on Version 7 Unix, as its default shell. Unix-like systems continue to have /bin/sh—which will be the Bourne shell, or a symbolic lin ...
, introduced with
Version 7 Unix
Version 7 Unix, also called Seventh Edition Unix, Version 7 or just V7, was an important early release of the Unix operating system. V7, released in 1979, was the last Bell Laboratories release to see widespread distribution before the commerc ...
in 1979, and has remained a characteristic of all later
Unix shell
A Unix shell is a Command-line_interface#Command-line_interpreter, command-line interpreter or shell (computing), shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command languag ...
s. The feature has since been adopted in other
programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s as well, including
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language".
Perl was developed ...
,
PHP,
Ruby
Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...
and Microsoft's
Powershell
PowerShell is a shell program developed by Microsoft for task automation and configuration management. As is typical for a shell, it provides a command-line interpreter for interactive use and a script interpreter for automation via a langu ...
under
Windows
Windows is a Product lining, product line of Proprietary software, proprietary graphical user interface, graphical operating systems developed and marketed by Microsoft. It is grouped into families and subfamilies that cater to particular sec ...
. 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
A child process (CP) in computing is a process created by another process (the parent process). This technique pertains to multitasking operating systems, and is sometimes called a subprocess or traditionally a subtask.
There are two major proce ...
to run the first command with its
standard output Standard may refer to:
Symbols
* Colours, standards and guidons, kinds of military signs
* Standard (emblem), a type of a large symbol or emblem used for identification
Norms, conventions or requirements
* Standard (metrology), an object t ...
piped back to the shell, which reads that output,
parsing
Parsing, syntax analysis, or syntactic analysis is a process of analyzing a String (computer science), string of Symbol (formal), symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal gramm ...
it into words separated by
whitespace
White space or whitespace may refer to:
Technology
* Whitespace characters, characters in computing that represent horizontal or vertical space
* White spaces (radio), allocated but locally unused radio frequencies
* TV White Space Database, a m ...
. 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
The C shell (csh or the improved version, tcsh) is a Unix shell created by Bill Joy while he was a graduate student at University of California, Berkeley in the late 1970s. It has been widely distributed, beginning with the 2BSD release of the ...
example shows how one might search for all the
C files containing the
string
String or strings may refer to:
*String (structure), a long flexible structure made from threads twisted together, which is used to tie, bind, or hang other objects
Arts, entertainment, and media Films
* ''Strings'' (1991 film), a Canadian anim ...
malloc
using
fgrep
grep is a command line interface, command-line utility for searching plaintext datasets for lines that match a regular expression. Its name comes from the ed (text editor), ed command g/re/p (global regular expression search and print), which has ...
and then edit any that are found using the
vi
editor. The syntactical notation shown here,
`
...
`
, using
backquotes as
delimiter
A delimiter is a sequence of one or more Character (computing), characters for specifying the boundary between separate, independent regions in plain text, Expression (mathematics), mathematical expressions or other Data stream, data streams. An ...
s, 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
In linguistics, syntax ( ) is the study of how words and morphemes combine to form larger units such as phrases and sentences. Central concerns of syntax include word order, grammatical relations, hierarchical sentence structure (constituenc ...
, how it's typed, and the
semantics
Semantics is the study of linguistic Meaning (philosophy), meaning. It examines what meaning is, how words get their meaning, and how the meaning of a complex expression depends on its parts. Part of this process involves the distinction betwee ...
, 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
KornShell (ksh) is a Unix shell which was developed by David Korn (computer scientist), David Korn at Bell Labs in the early 1980s and announced at USENIX Annual Technical Conference, USENIX on July 14, 1983. The initial development was base ...
(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
PowerShell is a shell program developed by Microsoft for task automation and configuration management. As is typical for a shell, it provides a command-line interpreter for interactive use and a script interpreter for automation via a langu ...
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
Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
systems where filenames never contained spaces but it doesn't work at all well on modern
Windows
Windows is a Product lining, product line of Proprietary software, proprietary graphical user interface, graphical operating systems developed and marketed by Microsoft. It is grouped into families and subfamilies that cater to particular sec ...
and
Linux
Linux ( ) is a family of open source Unix-like operating systems based on the Linux kernel, an kernel (operating system), operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically package manager, pac ...
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
PowerShell is a shell program developed by Microsoft for task automation and configuration management. As is typical for a shell, it provides a command-line interpreter for interactive use and a script interpreter for automation via a langu ...
:
$MyVariable = (ls)
echo $MyVariable
Expression substitution
A related facility, expression substitution, is found in the languages
Common Lisp
Common Lisp (CL) is a dialect of the Lisp programming language, published in American National Standards Institute (ANSI) standard document ''ANSI INCITS 226-1994 (S2018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperli ...
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
string literal or anonymous string is a literal for a string value in the source code of a computer program. Modern programming languages commonly use a quoted sequence of characters, formally "bracketed delimiters", as in x = "foo", where , "foo ...
). For example, the ABC command produces the output .
See also
*
Process substitution
References
{{DEFAULTSORT:Command Substitution
Programming language topics
Unix programming tools