ArticleslgStudy

science

Command substitution

Command substitution is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Command substitution rather than just read about it. In short: 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.

Key takeaways

  • Command substitution belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Command substitution to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Command substitution from memory before moving on to harder problems.

Reference excerpt

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.

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.

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:

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.

See also Process substitution

References

Worked examples

Example 1 — a first encounter with Command substitution

Start with the simplest possible case. Write down what Command substitution claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Command substitution before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Command substitution ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Command substitution

In research
Command substitution appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Command substitution in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Command substitution is common in secondary-school and first-year university syllabi. It links to neighbouring topics Programming language topics, Unix programming tools, so understanding it makes those chapters shorter.
In everyday life
Look for Command substitution outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Command substitution” →

How to study Command substitution in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Command substitution means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Command substitution out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Command substitution in simple terms?

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 o…

Why does Command substitution matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Command substitution?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Command substitution.

Tags

  • Programming language topics
  • Unix programming tools

Keep exploring