What is “Parameter expansion” (A.K.A “Variable expansion”) in shell-scripting in general and in Bash in particular?

The name of the pictureThe name of the pictureThe name of the pictureClash 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:



  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?










share|improve this question



















  • 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















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:



  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?










share|improve this question



















  • 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













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:



  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?










share|improve this question















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?







variable terminology






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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













  • 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











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.






share|improve this answer






















  • 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
    yesterday

















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.






share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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






























    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.






    share|improve this answer






















    • 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
      yesterday














    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.






    share|improve this answer






















    • 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
      yesterday












    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.






    share|improve this answer














    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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 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
      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











    • @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















    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












    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.






    share|improve this answer


























      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.






      share|improve this answer
























        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.






        share|improve this answer














        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '17 at 5:53









        Arcticooling

        83124




        83124










        answered Nov 11 '17 at 10:57









        bu5hman

        1,174214




        1,174214



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay