Command substitution
Encyclopedia
In computing
, command substitution is a facility originally introduced in the Unix shell
s that allows a command to be run and its output to be pasted back on the command line as arguments to another command. Shells typically do this 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.
example shows how one might search for all the C
files containing the string
as delimiter
s, is the original style and is supported by all the common Unix shells.
While very easy to type (an important factor for an interactive command processor) the backquote notation has been criticized for being awkward to nest, putting one command substitution inside another, because both the left and the right delimiters are the same. To solve this problem, bash 2.0 and the Korn shell
(ksh)
introduced an alternative notation,
:
, introduced with Unix 7th Edition, released in 1979, and has remained a characteristic of all later Unix shells. The feature has since been adopted in the programming language
s Mythryl, Perl
, PHP
, and Ruby
. It appears in Microsoft's cmd.exe
under Windows, albeit only as part of the
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 .
In a sense this is the inverse of the eval
function (in languages that have one): expression substitution turns an expression into a string, and eval turns a string into an expression.
Computing
Computing is usually defined as the activity of using and improving computer hardware and software. It is the computer-specific part of information technology...
, command substitution is a facility originally introduced in the Unix shell
Unix shell
A Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and for Unix-like systems...
s that allows a command to be run and its output to be pasted back on the command line as arguments to another command. Shells typically do this by creating a child process
Child process
A child process in computing is a process created by another process .A child process inherits most of its attributes, such as open files, from its parent. In UNIX, a child process is in fact created as a copy of the parent...
to run the first command with its standard output piped
Pipeline (Unix)
In Unix-like computer operating systems , a pipeline is the original software pipeline: a set of processes chained by their standard streams, so that the output of each process feeds directly as input to the next one. Each connection is implemented by an anonymous pipe...
back to the shell, which reads that output, parsing
Parsing
In computer science and linguistics, parsing, or, more formally, syntactic analysis, is the process of analyzing a text, made of a sequence of tokens , to determine its grammatical structure with respect to a given formal grammar...
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.
Examples
This C shellC shell
The C shell is a Unix shell that was created by Bill Joy while a graduate student at University of California, Berkeley in the late 1970s. It has been distributed widely, beginning with the 2BSD release of the BSD Unix system that Joy began distributing in 1978...
example shows how one might search for all the C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
files containing the string
String (computer science)
In formal languages, which are used in mathematical logic and theoretical computer science, a string is a finite sequence of symbols that are chosen from a set or alphabet....
malloc
using fgrep
and then edit any that are found using the viVivi is a screen-oriented text editor originally created for the Unix operating system. The portable subset of the behavior of vi and programs based on it, and the ex editor language supported within these programs, is described by the Single Unix Specification and POSIX.The original code for vi...
editor. The syntactical notation shown here, `
... `
, using backquotesGrave accent
The grave accent is a diacritical mark used in written Breton, Catalan, Corsican, Dutch, French, Greek , Italian, Mohawk, Norwegian, Occitan, Portuguese, Scottish Gaelic, Vietnamese, Welsh, Romansh, and other languages.-Greek:The grave accent was first used in the polytonic orthography of Ancient...
as delimiter
Delimiter
A delimiter is a sequence of one or more characters used to specify the boundary between separate, independent regions in plain text or other data streams. An example of a delimiter is the comma character, which acts as a field delimiter in a sequence of comma-separated values.Delimiters represent...
s, is the original style and is supported by all the common Unix shells.
While very easy to type (an important factor for an interactive command processor) the backquote notation has been criticized for being awkward to nest, putting one command substitution inside another, because both the left and the right delimiters are the same. To solve this problem, bash 2.0 and the Korn shell
Korn shell
The Korn shell is a Unix shell which was developed by David Korn in the early 1980s and announced at USENIX on July 14, 1983. Other early contributors were AT&T Bell Labs developers Mike Veach, who wrote the emacs code, and Pat Sullivan, who wrote the vi code...
(ksh)
introduced an alternative notation,
$(
... )
, borrowing from the notational style used for variable substitutionVariable (programming)
In computer programming, a variable is a symbolic name given to some known or unknown quantity or information, for the purpose of allowing the name to be used independently of the information it represents...
:
History
Command substitution first appeared in the Bourne shellBourne shell
The Bourne shell, or sh, was the default Unix shell of Unix Version 7 and most Unix-like systems continue to have /bin/sh - which will be the Bourne shell, or a symbolic link or hard link to a compatible shell - even when more modern shells are used by most users.Developed by Stephen Bourne at AT&T...
, introduced with Unix 7th Edition, released in 1979, and has remained a characteristic of all later Unix shells. The feature has since been adopted in the programming language
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....
s Mythryl, Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...
, PHP
PHP
PHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document...
, and Ruby
Ruby (programming language)
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...
. It appears in Microsoft's cmd.exe
Cmd.exe
Command Prompt is the Microsoft-supplied command-line interpreter on OS/2, Windows CE and on Windows NT-based operating systems...
under Windows, albeit only as part of the
for
command.Expression substitution
A related facility, expression substitution, is found in the languages Common LispCommon Lisp
Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . From the ANSI Common Lisp standard the Common Lisp HyperSpec has been derived for use with web browsers...
and Scheme
Scheme
Scheme may refer to:* Scheme , a minimalist, multi-paradigm dialect of Lisp* Scheme , a concept in algebraic geometry* Scheme , a figure of speech that changes a sentence's structure-See also:...
, 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 .
In a sense this is the inverse of the eval
Eval
In some programming languages, eval is a function which evaluates a string as though it were an expression and returns a result; in others, it executes multiple lines of code as though they had been included instead of the line including the eval...
function (in languages that have one): expression substitution turns an expression into a string, and eval turns a string into an expression.