Command substitution

Command substitution

In computing, command substitution is a facility originally introduced in the Unix shells 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.

Contents

Examples

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`

While very easy to type (an important factor for an interactive command processor) the backquote notation has been criticized[1] 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[2] and the Korn shell (ksh)[3] introduced an alternative notation, $( ... ), borrowing from the notational style used for variable substitution:

#!/bin/bash
vi $(fgrep -l malloc *.c)

History

Command substitution first appeared in the Bourne shell,[4] 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 languages Mythryl, Perl, PHP, and Ruby. It appears in Microsoft's cmd.exe under Windows, albeit only as part of the for command.

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 WRITE '2 + 2 = `2+2`' produces the output 2 + 2 = 4.

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.

See Also

  • Process substitution

References

  1. ^ Unix Power Tools: 45.31 Nested Command Substitution.
  2. ^ Bash Prompt HOWTO: 3.3. Command Substitution.
  3. ^ Rosenblatt, Bill; Arnold Robbins (2002). Learning the Korn Shell (2 ed.). O'Reilly Media, Inc.. p. 127. ISBN 9780596001957. http://books.google.com/books?hl=en&lr=&id=5nMCY272chUC. Retrieved 2010-07-20. "The syntax of command substitution is: $(Unix command) The command inside the parenthesis is run, and anything the command writes to standard output (and to standard error) is returned as the value of the expression." 
  4. ^ Dahdah, Howard. The A-Z of Programming Languages: Bourne shell, or sh, An in-depth interview with Steve Bourne, creator of the Bourne shell, or sh, Computerworld, March 5, 2009.

Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • COMMAND.COM — Тип Командный интерпретатор Разработчик Seattle Computer Products, Microsoft, IBM, Novell и др. Операционная система DOS (MS DOS, DR DOS, FreeDOS и т. д.), OS/2, Windows 9x и др. Язык …   Википедия

  • Comparison of command shells — For more details on this topic, see Shell (computing). A command shell is a command line interface computer program to an operating system. Contents 1 General characteristics 2 Interactive features 3 Programming features …   Wikipedia

  • Tool Command Language — Pour les articles homonymes, voir Tcl. Tcl Apparu en …   Wikipédia en Français

  • Friendly interactive shell — infobox software name = fish caption = The friendly interactive shell author = Axel Liljencrantz developer = Axel Liljencrantz released = latest release version = 1.23.0 latest release date = 13 January 2008 operating system = Unix like genre =… …   Wikipedia

  • Make (software) — make Original author(s) Stuart Feldman Initial release 1977 Type build automation tool In software development, Make is a utility that automatically builds executable programs and libraries from source code by rea …   Wikipedia

  • Bash — Infobox Software name = Bash caption = Screenshot of bash and sh sessions demonstrating some features developer = Chet Ramey latest release version = 3.2.39 latest release date = release date|2008|04|30 programming language = C operating system …   Wikipedia

  • Amiga support and maintenance software — This article is a split of main article Amiga software and refers to any support and maintenance software that run on Amiga line of computers.See also related articles Amiga productivity software and Amiga Internet and communications software for …   Wikipedia

  • BASH — Die Unix Shell oder kurz Shell (en. Hülle, Schale) bezeichnet die traditionelle Benutzerschnittstelle unter Unix oder unixoiden Betriebssystemen. Der Benutzer kann in einer Eingabezeile Kommandos eintippen, die der Computer dann sogleich ausführt …   Deutsch Wikipedia

  • Bourne-Again-Shell — Die Unix Shell oder kurz Shell (en. Hülle, Schale) bezeichnet die traditionelle Benutzerschnittstelle unter Unix oder unixoiden Betriebssystemen. Der Benutzer kann in einer Eingabezeile Kommandos eintippen, die der Computer dann sogleich ausführt …   Deutsch Wikipedia

  • Bourne-Shell — Die Unix Shell oder kurz Shell (en. Hülle, Schale) bezeichnet die traditionelle Benutzerschnittstelle unter Unix oder unixoiden Betriebssystemen. Der Benutzer kann in einer Eingabezeile Kommandos eintippen, die der Computer dann sogleich ausführt …   Deutsch Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”