substituting a value of a given variable in another.
That description is wrong on several counts. Variable substitution replaces the name of a variable (plus some syntactic fluff) by its value. Furthermore, it does not operate “in” a variable, but in a command. The command could be one that sets the value of a variable, but that's just one case among many.
For example, the command echo $foo
displays the value of the variable. The source code contains $foo
, and the corresponding output contains the value of the variable foo
.
The reason this is called “variable substitution” is that the shell operates by a series of transformations of strings (and lists of strings). For example (simplified), consider the command ls -l $dir/*.$ext
. To evaluate it, several things happen in sequence:
- The shell starts to parse the command and splits it into three words:
ls
, -l
and $dir/*.$ext
.
- In the third word, the shell sees two variable substitutions to perform (that's what the dollar signs mean in this context). Say that the value of
dir
is /some/path
and the value of ext
is txt
, then the shell rewrites $dir/*.$ext
to /some/path/*.txt
. This is a substitution because the value of each variable is substituted for the dollar-name syntax.
- The shell expands the wildcard pattern
/some/path/*.txt
to the list of matching file names.
- The shell executes
ls
with the arguments that it's computed.
(The syntax $foo
does more than substitute the value of a variable but that's another story.)
In most programming languages, to take the value of a variable, you just write the variable's name. The shell is designed for interactive use; if you write a name it's interpreted as a literal string. That's why the syntax to take the value of a variable has an extra marker to say “I want to take a variable's value”.
why this action requires a special term instead just saying "changing a variable's value with editing it manually in a text editor?
Variable substitution has nothing to do with changing the value of a variable. Changing the value of a variable is an assignment.
Of course an assignment can contain variable substitutions, like any other command. But variable substitutions are not designed specifically for assignments.
Furthermore you can't change the value of a variable with an editor. An already declared variable has a value in each process, it isn't a system configuration. You can have configuration files that set the initial value of a variable, and you can use an editor to change that initial value in those files, but after that the value can change.
echo '$PWD'
print$PWD
and not my current directory?" here on U&L, I need the term variable substitution to be able to say "It's because variable substitution doesn't happen within single-quoted strings." – Kusalananda May 19 '17 at 17:57for i in {1..10};
) and glob expansion (for file in dir/*log;
) for example, fall under the same umbrella as expansion of variables (for arg in "$@";
) or subshells (for ip in $(awk '$1 !~ /^#/ {print $1}' /etc/hosts);
. – DopeGhoti May 19 '17 at 19:07