This question is very basic and I ask it not only for myself but for more newcomers who see the term "Variable substitution" and having the following thoughts:

As far as I understand, the term variable substitution deals with "substituting a value of a given variable in another".

Why is this action need a special term, why can't we just say "changing a variable's value with editing it manually in Vim or Nano or sed"?

The above question isn't what I'm asking, it's but example to what I asked myself. This is my actual question:

Why is there a "Variable substitution" concept in Bash? And if you answer, can you please give a practical example for what types of actions it uses you in your work.

  • 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

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 is this action need a special term, why can't we just say a Bash programmer changes a variable's value with editing it manually in Vim or Nano or sed?

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. A 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, but after that the value can change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.