metacharacter processing for default value in variable expansion

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












When a default value is provided for a variable expansion (e.g. $varname-default), how are the characters that make up default meant to be interpreted?



Naively, I would have guessed that variable substitution (e.g. $b), and expansion of quote-like characters ', ", would occur in this context ... but it appears that expansion of quote-like characters does not always occur.



For example,



> echo x$a-''y
xy


but



> echo x"$a-''"y 
x''y


I expected xy in both cases, since construction that expand variables like $a-$PATH and ( BRACE='}'; echo $a-$BRACE ), work fine.



However GNU bash, version 4.4.19(1)-release and ... the latest version of mksh both have the same behavior where '' is included literally in the output for the second example.










share|improve this question

















  • 1




    It's expected behavior and in POSIX spec pubs.opengroup.org/onlinepubs/9699919799/utilities/…
    – cuonglm
    Aug 17 at 3:30














up vote
0
down vote

favorite












When a default value is provided for a variable expansion (e.g. $varname-default), how are the characters that make up default meant to be interpreted?



Naively, I would have guessed that variable substitution (e.g. $b), and expansion of quote-like characters ', ", would occur in this context ... but it appears that expansion of quote-like characters does not always occur.



For example,



> echo x$a-''y
xy


but



> echo x"$a-''"y 
x''y


I expected xy in both cases, since construction that expand variables like $a-$PATH and ( BRACE='}'; echo $a-$BRACE ), work fine.



However GNU bash, version 4.4.19(1)-release and ... the latest version of mksh both have the same behavior where '' is included literally in the output for the second example.










share|improve this question

















  • 1




    It's expected behavior and in POSIX spec pubs.opengroup.org/onlinepubs/9699919799/utilities/…
    – cuonglm
    Aug 17 at 3:30












up vote
0
down vote

favorite









up vote
0
down vote

favorite











When a default value is provided for a variable expansion (e.g. $varname-default), how are the characters that make up default meant to be interpreted?



Naively, I would have guessed that variable substitution (e.g. $b), and expansion of quote-like characters ', ", would occur in this context ... but it appears that expansion of quote-like characters does not always occur.



For example,



> echo x$a-''y
xy


but



> echo x"$a-''"y 
x''y


I expected xy in both cases, since construction that expand variables like $a-$PATH and ( BRACE='}'; echo $a-$BRACE ), work fine.



However GNU bash, version 4.4.19(1)-release and ... the latest version of mksh both have the same behavior where '' is included literally in the output for the second example.










share|improve this question













When a default value is provided for a variable expansion (e.g. $varname-default), how are the characters that make up default meant to be interpreted?



Naively, I would have guessed that variable substitution (e.g. $b), and expansion of quote-like characters ', ", would occur in this context ... but it appears that expansion of quote-like characters does not always occur.



For example,



> echo x$a-''y
xy


but



> echo x"$a-''"y 
x''y


I expected xy in both cases, since construction that expand variables like $a-$PATH and ( BRACE='}'; echo $a-$BRACE ), work fine.



However GNU bash, version 4.4.19(1)-release and ... the latest version of mksh both have the same behavior where '' is included literally in the output for the second example.







shell






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 17 at 3:22









Gregory Nisbet

1,222818




1,222818







  • 1




    It's expected behavior and in POSIX spec pubs.opengroup.org/onlinepubs/9699919799/utilities/…
    – cuonglm
    Aug 17 at 3:30












  • 1




    It's expected behavior and in POSIX spec pubs.opengroup.org/onlinepubs/9699919799/utilities/…
    – cuonglm
    Aug 17 at 3:30







1




1




It's expected behavior and in POSIX spec pubs.opengroup.org/onlinepubs/9699919799/utilities/…
– cuonglm
Aug 17 at 3:30




It's expected behavior and in POSIX spec pubs.opengroup.org/onlinepubs/9699919799/utilities/…
– cuonglm
Aug 17 at 3:30










1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










The $parameter:-[word] expansion is subject to several expansions:



From man bash:




In each of the cases below, word is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.




From POSIX spec:




word shall be subjected to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.




Both report the same, and both do not include "quote removal".



The quotes get removed after the expansion of the whole variable has already been done. However, it is difficult to show exactly how that is done:



$ sh
$ unset b
$ set -x
+ set -x
$ echo 1 $b-e
+ echo 1 e
1 e
$ echo 2 $b-"e"
+ echo 2 e
2 e
$ echo 3 $b-'e'
+ echo 3 e
3 e
$ echo 4 "$b-e"
+ echo 4 e
4 e
$ echo 5 "$b-"e""
+ echo 5 e
5 e
$ echo 6 "$b-'e'"
+ echo 6 'e'
6 'e'
$ set +x
+ set +x


Or:



$ $ set -x; echo 1 x$b-ey; echo 2 x$b-"e"y; echo 3 x$b-'e'y; echo 4 "x$b-ey"; echo 5 "x$b-"e"y"; echo 6 "x$b-'e'y"; set +x
+ echo 1 xey
1 xey
+ echo 2 xey
2 xey
+ echo 3 xey
3 xey
+ echo 4 xey
4 xey
+ echo 5 xey
5 xey
+ echo 6 x'e'y
6 x'e'y
+ set +x
$





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%2f463104%2fmetacharacter-processing-for-default-value-in-variable-expansion%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    The $parameter:-[word] expansion is subject to several expansions:



    From man bash:




    In each of the cases below, word is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.




    From POSIX spec:




    word shall be subjected to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.




    Both report the same, and both do not include "quote removal".



    The quotes get removed after the expansion of the whole variable has already been done. However, it is difficult to show exactly how that is done:



    $ sh
    $ unset b
    $ set -x
    + set -x
    $ echo 1 $b-e
    + echo 1 e
    1 e
    $ echo 2 $b-"e"
    + echo 2 e
    2 e
    $ echo 3 $b-'e'
    + echo 3 e
    3 e
    $ echo 4 "$b-e"
    + echo 4 e
    4 e
    $ echo 5 "$b-"e""
    + echo 5 e
    5 e
    $ echo 6 "$b-'e'"
    + echo 6 'e'
    6 'e'
    $ set +x
    + set +x


    Or:



    $ $ set -x; echo 1 x$b-ey; echo 2 x$b-"e"y; echo 3 x$b-'e'y; echo 4 "x$b-ey"; echo 5 "x$b-"e"y"; echo 6 "x$b-'e'y"; set +x
    + echo 1 xey
    1 xey
    + echo 2 xey
    2 xey
    + echo 3 xey
    3 xey
    + echo 4 xey
    4 xey
    + echo 5 xey
    5 xey
    + echo 6 x'e'y
    6 x'e'y
    + set +x
    $





    share|improve this answer


























      up vote
      1
      down vote



      accepted










      The $parameter:-[word] expansion is subject to several expansions:



      From man bash:




      In each of the cases below, word is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.




      From POSIX spec:




      word shall be subjected to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.




      Both report the same, and both do not include "quote removal".



      The quotes get removed after the expansion of the whole variable has already been done. However, it is difficult to show exactly how that is done:



      $ sh
      $ unset b
      $ set -x
      + set -x
      $ echo 1 $b-e
      + echo 1 e
      1 e
      $ echo 2 $b-"e"
      + echo 2 e
      2 e
      $ echo 3 $b-'e'
      + echo 3 e
      3 e
      $ echo 4 "$b-e"
      + echo 4 e
      4 e
      $ echo 5 "$b-"e""
      + echo 5 e
      5 e
      $ echo 6 "$b-'e'"
      + echo 6 'e'
      6 'e'
      $ set +x
      + set +x


      Or:



      $ $ set -x; echo 1 x$b-ey; echo 2 x$b-"e"y; echo 3 x$b-'e'y; echo 4 "x$b-ey"; echo 5 "x$b-"e"y"; echo 6 "x$b-'e'y"; set +x
      + echo 1 xey
      1 xey
      + echo 2 xey
      2 xey
      + echo 3 xey
      3 xey
      + echo 4 xey
      4 xey
      + echo 5 xey
      5 xey
      + echo 6 x'e'y
      6 x'e'y
      + set +x
      $





      share|improve this answer
























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        The $parameter:-[word] expansion is subject to several expansions:



        From man bash:




        In each of the cases below, word is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.




        From POSIX spec:




        word shall be subjected to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.




        Both report the same, and both do not include "quote removal".



        The quotes get removed after the expansion of the whole variable has already been done. However, it is difficult to show exactly how that is done:



        $ sh
        $ unset b
        $ set -x
        + set -x
        $ echo 1 $b-e
        + echo 1 e
        1 e
        $ echo 2 $b-"e"
        + echo 2 e
        2 e
        $ echo 3 $b-'e'
        + echo 3 e
        3 e
        $ echo 4 "$b-e"
        + echo 4 e
        4 e
        $ echo 5 "$b-"e""
        + echo 5 e
        5 e
        $ echo 6 "$b-'e'"
        + echo 6 'e'
        6 'e'
        $ set +x
        + set +x


        Or:



        $ $ set -x; echo 1 x$b-ey; echo 2 x$b-"e"y; echo 3 x$b-'e'y; echo 4 "x$b-ey"; echo 5 "x$b-"e"y"; echo 6 "x$b-'e'y"; set +x
        + echo 1 xey
        1 xey
        + echo 2 xey
        2 xey
        + echo 3 xey
        3 xey
        + echo 4 xey
        4 xey
        + echo 5 xey
        5 xey
        + echo 6 x'e'y
        6 x'e'y
        + set +x
        $





        share|improve this answer














        The $parameter:-[word] expansion is subject to several expansions:



        From man bash:




        In each of the cases below, word is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.




        From POSIX spec:




        word shall be subjected to tilde expansion, parameter expansion, command substitution, and arithmetic expansion.




        Both report the same, and both do not include "quote removal".



        The quotes get removed after the expansion of the whole variable has already been done. However, it is difficult to show exactly how that is done:



        $ sh
        $ unset b
        $ set -x
        + set -x
        $ echo 1 $b-e
        + echo 1 e
        1 e
        $ echo 2 $b-"e"
        + echo 2 e
        2 e
        $ echo 3 $b-'e'
        + echo 3 e
        3 e
        $ echo 4 "$b-e"
        + echo 4 e
        4 e
        $ echo 5 "$b-"e""
        + echo 5 e
        5 e
        $ echo 6 "$b-'e'"
        + echo 6 'e'
        6 'e'
        $ set +x
        + set +x


        Or:



        $ $ set -x; echo 1 x$b-ey; echo 2 x$b-"e"y; echo 3 x$b-'e'y; echo 4 "x$b-ey"; echo 5 "x$b-"e"y"; echo 6 "x$b-'e'y"; set +x
        + echo 1 xey
        1 xey
        + echo 2 xey
        2 xey
        + echo 3 xey
        3 xey
        + echo 4 xey
        4 xey
        + echo 5 xey
        5 xey
        + echo 6 x'e'y
        6 x'e'y
        + set +x
        $






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Aug 17 at 5:34

























        answered Aug 17 at 4:48









        Isaac

        7,1241835




        7,1241835



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f463104%2fmetacharacter-processing-for-default-value-in-variable-expansion%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