3

I understand the term "Parameter expansion" (A.K.A "Variable expansion") to be an umbrella term for several unrelated operations in shell-scripting in general and in Bash in particular, such as:

  1. Variable substitution.
  2. Line splitting in case of 2 or more values in the same line.
  3. Globbing a variable of a particular scope.

and maybe more.

If I understand the philosophy behind this term correctly, we "expand" the variable from being just a variable, to be a more influencing stream of data (say, a substituted value) - it's now expanded to be a variable that something was done with.

My question:

Is my understanding of term accurate enough in general and are there any more operations under this umbrella term that should be mentioned in an answer?

2 Answers2

4

Parameter expansion also known as variable expansion is the replacement of syntactic structures of the form $parameter and ${parameter} with the value of the parameter, possibly processed by the application of modifiers.

When a shell reads a command line it processes it through several well defined steps, such as splitting the line into words, performing several kinds of expansions (of which parameter expansion is but one), and applying any redirections. For example the Bash shell will first split the line into words. then it will perform brace expansion, tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution and process expansion in left-to-right order, followed by final word splitting and pathname expansion.

I don't understand what you mean by "variable printing".

Globbing, or "pathname expansion" as the Bash documentation prefers to call it, happens after variable expansion; the shell examines each word on the command line for the presence of the characters *, ? and [, and, if any of them is found, considers the word to be a pattern and replaces it with the list of matching files, subject to certain configuration options.

See the manual page of your shell for details; for example, for Bash, you can read the manual page, the Reference Manual, the Advanced Scripting Guide and many other materials.

ilkkachu
  • 138,973
AlexP
  • 10,455
  • By "printing" I meant printing the value as with echo. I've now edited the question to clarify that. – Arcticooling Nov 11 '17 at 11:07
  • @Arcticooling: echo is a command like any other. There is nothing special in the processing of its arguments. I have no idea why you would think that the arguments of echo receive some kind of special treatment. – AlexP Nov 11 '17 at 14:43
  • Note that variables are some forms of parameters. They are the ones you can set with assignment to hold some data to be reused. Shells initially didn't have variables. The Thompson shell had positional parameters ($1...) but no variables for instance. – Stéphane Chazelas Oct 07 '18 at 18:58
-1

To my understanding there is no such thing as variable expansion.

There are variables and shell (parameter) expansion (see here).

A variable is a string or number which you reference by giving it a name:

myVariable=2

Shell parameter expansion is the process by which the shell takes input and applies certain rules to it before execution. So while:

echo $myVariable

is not a valid command for execution as bash 'knows' that when it sees a $ followed by some text, it needs to refer to its list of variables to find out that the current value of $myVariable is in fact 2; Only then you should execute:

echo 2

Understanding the whole shebang (sic) of shell expansion is simply a matter of understanding the set of rules that bash applies to expand certain syntax and how this can be modified when using double quotes (""), single quotes ('') or back ticks (``).

I recommend a careful read through of the relevant parts of the bash reference manual together with some practice at the terminal.

bu5hman
  • 4,756