0

This question is very basic and I ask it not only for myself but for other newcomers who see the term variable substitution and have the following thoughts:

As far as I understand, the term variable substitution describes "substituting a value of a variable in another" but I doubt that's correct.
I wondered why this action requires a special term instead just saying "changing a variable's value with editing it manually in a text editor?

What is variable substitution concept in shell programming and if you know the answer, please give a practical example for what types of actions it uses you in your work.

  • 1
    When answering the question "Why does 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:57
  • I believe that this is because the expansions and substitutions are only in part variables. Brace expansion (for 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
  • I edited the question more than 2 years after asking it to make the phrasing better; if the downvoter knew that - I personally think downvoting was a wrong move. –  Sep 12 '19 at 06:02

1 Answers1

6

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:

  1. The shell starts to parse the command and splits it into three words: ls, -l and $dir/*.$ext.
  2. 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.
  3. The shell expands the wildcard pattern /some/path/*.txt to the list of matching file names.
  4. 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.