What is âParameter expansionâ (A.K.A âVariable expansionâ) in shell-scripting in general and in Bash in particular?
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
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:
- Variable substitution.
- Line splitting in case of 2 or more values in the same line.
- 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?
variable terminology
add a comment |Â
up vote
1
down vote
favorite
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:
- Variable substitution.
- Line splitting in case of 2 or more values in the same line.
- 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?
variable terminology
1
Related: pubs.opengroup.org/onlinepubs/9699919799/idx/shell.html
â Kusalananda
Nov 11 '17 at 10:33
Context: this is a follow-up to What is the difference between expanding a variable and printing it (as with echo or printf)?
â Stephen Kitt
Nov 11 '17 at 11:36
As explained previously, start by forgetting about printing â thatâÂÂs not done by the shell, and therefore isnâÂÂt part of expansion.
â Stephen Kitt
Nov 11 '17 at 12:32
Thanks @StephenKitt so if it's not done by the shell, is printing just a case of substitution?
â Arcticooling
Nov 11 '17 at 22:39
Oh and @StephenKitt your answer on this will be very much appreciated. Reading it will be like finding a large shining golden treasure hidden in an old mesoamerican temple covered with exotic plants for me.
â Arcticooling
Nov 11 '17 at 22:53
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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:
- Variable substitution.
- Line splitting in case of 2 or more values in the same line.
- 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?
variable terminology
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:
- Variable substitution.
- Line splitting in case of 2 or more values in the same line.
- 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?
variable terminology
variable terminology
edited yesterday
JohnDoea
97728
97728
asked Nov 11 '17 at 6:25
Arcticooling
83124
83124
1
Related: pubs.opengroup.org/onlinepubs/9699919799/idx/shell.html
â Kusalananda
Nov 11 '17 at 10:33
Context: this is a follow-up to What is the difference between expanding a variable and printing it (as with echo or printf)?
â Stephen Kitt
Nov 11 '17 at 11:36
As explained previously, start by forgetting about printing â thatâÂÂs not done by the shell, and therefore isnâÂÂt part of expansion.
â Stephen Kitt
Nov 11 '17 at 12:32
Thanks @StephenKitt so if it's not done by the shell, is printing just a case of substitution?
â Arcticooling
Nov 11 '17 at 22:39
Oh and @StephenKitt your answer on this will be very much appreciated. Reading it will be like finding a large shining golden treasure hidden in an old mesoamerican temple covered with exotic plants for me.
â Arcticooling
Nov 11 '17 at 22:53
add a comment |Â
1
Related: pubs.opengroup.org/onlinepubs/9699919799/idx/shell.html
â Kusalananda
Nov 11 '17 at 10:33
Context: this is a follow-up to What is the difference between expanding a variable and printing it (as with echo or printf)?
â Stephen Kitt
Nov 11 '17 at 11:36
As explained previously, start by forgetting about printing â thatâÂÂs not done by the shell, and therefore isnâÂÂt part of expansion.
â Stephen Kitt
Nov 11 '17 at 12:32
Thanks @StephenKitt so if it's not done by the shell, is printing just a case of substitution?
â Arcticooling
Nov 11 '17 at 22:39
Oh and @StephenKitt your answer on this will be very much appreciated. Reading it will be like finding a large shining golden treasure hidden in an old mesoamerican temple covered with exotic plants for me.
â Arcticooling
Nov 11 '17 at 22:53
1
1
Related: pubs.opengroup.org/onlinepubs/9699919799/idx/shell.html
â Kusalananda
Nov 11 '17 at 10:33
Related: pubs.opengroup.org/onlinepubs/9699919799/idx/shell.html
â Kusalananda
Nov 11 '17 at 10:33
Context: this is a follow-up to What is the difference between expanding a variable and printing it (as with echo or printf)?
â Stephen Kitt
Nov 11 '17 at 11:36
Context: this is a follow-up to What is the difference between expanding a variable and printing it (as with echo or printf)?
â Stephen Kitt
Nov 11 '17 at 11:36
As explained previously, start by forgetting about printing â thatâÂÂs not done by the shell, and therefore isnâÂÂt part of expansion.
â Stephen Kitt
Nov 11 '17 at 12:32
As explained previously, start by forgetting about printing â thatâÂÂs not done by the shell, and therefore isnâÂÂt part of expansion.
â Stephen Kitt
Nov 11 '17 at 12:32
Thanks @StephenKitt so if it's not done by the shell, is printing just a case of substitution?
â Arcticooling
Nov 11 '17 at 22:39
Thanks @StephenKitt so if it's not done by the shell, is printing just a case of substitution?
â Arcticooling
Nov 11 '17 at 22:39
Oh and @StephenKitt your answer on this will be very much appreciated. Reading it will be like finding a large shining golden treasure hidden in an old mesoamerican temple covered with exotic plants for me.
â Arcticooling
Nov 11 '17 at 22:53
Oh and @StephenKitt your answer on this will be very much appreciated. Reading it will be like finding a large shining golden treasure hidden in an old mesoamerican temple covered with exotic plants for me.
â Arcticooling
Nov 11 '17 at 22:53
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
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.
By "printing" I meant printing the value as withecho
. 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 ofecho
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
yesterday
add a comment |Â
up vote
0
down vote
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.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
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.
By "printing" I meant printing the value as withecho
. 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 ofecho
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
yesterday
add a comment |Â
up vote
2
down vote
accepted
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.
By "printing" I meant printing the value as withecho
. 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 ofecho
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
yesterday
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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.
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.
edited yesterday
ilkkachu
52.7k679145
52.7k679145
answered Nov 11 '17 at 10:38
AlexP
6,716924
6,716924
By "printing" I meant printing the value as withecho
. 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 ofecho
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
yesterday
add a comment |Â
By "printing" I meant printing the value as withecho
. 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 ofecho
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
yesterday
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
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
@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
yesterday
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
yesterday
add a comment |Â
up vote
0
down vote
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.
add a comment |Â
up vote
0
down vote
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.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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.
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.
edited Nov 14 '17 at 5:53
Arcticooling
83124
83124
answered Nov 11 '17 at 10:57
bu5hman
1,174214
1,174214
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f403867%2fwhat-is-parameter-expansion-a-k-a-variable-expansion-in-shell-scripting-in%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Related: pubs.opengroup.org/onlinepubs/9699919799/idx/shell.html
â Kusalananda
Nov 11 '17 at 10:33
Context: this is a follow-up to What is the difference between expanding a variable and printing it (as with echo or printf)?
â Stephen Kitt
Nov 11 '17 at 11:36
As explained previously, start by forgetting about printing â thatâÂÂs not done by the shell, and therefore isnâÂÂt part of expansion.
â Stephen Kitt
Nov 11 '17 at 12:32
Thanks @StephenKitt so if it's not done by the shell, is printing just a case of substitution?
â Arcticooling
Nov 11 '17 at 22:39
Oh and @StephenKitt your answer on this will be very much appreciated. Reading it will be like finding a large shining golden treasure hidden in an old mesoamerican temple covered with exotic plants for me.
â Arcticooling
Nov 11 '17 at 22:53