Talk:Substitution

From LQWiki
Jump to navigation Jump to search

This hardly seems complete coverage of

 command substitution
 process substitution

and seems more like a treatment of the various kinds of "expansion" which are not named as such here. -- 4dummies

what is command substition? What is process substitution?
echo $(ls)
substitutes ls by "foo.bar". I call this substitution. Do you call it expansion? Why? --ThorstenStaerk 18:39, May 8, 2011 (UTC)

I take the terminology from the (Gentoo) bash(1) man page, which says, in part:

EXPANSION
      Expansion is performed on the command line after it has been split into
      words.  There are seven kinds of expansion performed: brace  expansion,
      tilde  expansion,  parameter  and variable expansion, command substitu‐
      tion, arithmetic expansion, word splitting, and pathname expansion.
      The order of expansions is: brace expansion, tilde  expansion,  parame‐
      ter,  variable  and arithmetic expansion and command substitution (done
      in a left-to-right fashion), word splitting, and pathname expansion.
      On systems that can support it, there is an additional expansion avail‐
      able: process substitution.

From this I take it that the overall category is expansion, and has two that are specifically called substitutions: of commands and processes, and one that's called word splitting. In spite of the above, there is the additional heading HISTORY EXPANSION, like the command "!!"


Command substitution is either of the backtick `` or $() forms.

Thanks to your question, I now actually have a clue about process substitution, which I did not know was even possible. My shell scripts will accordingly become even more arcane and unreadable. Example:

 ls |sort

is equivalent to the process substitution

 sort <(ls)

One can also try

 sort <(echo 2) <(echo 1)

to combine inputs, though in this case sort would ignore stdin completely because these substitutions are turned into filenames. Try

 echo <(echo 1)

and compare to

 cat <(echo 1)


The example of "ls *" is usually called "file globbing" or just "globbing", but is documented under the headline "Pathname Expansion", but is not a kind of "substitution".


-- 4dummies 20:21, May 8, 2011 (UTC)