Expanding a variable containing an equals sign and curly braces

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











up vote
1
down vote

favorite
1












I'm writing a script that uses rsync and excludes files based on certain settings. I end up with an exclude flag that has a format of --exclude=foo, bar, baz.



However, upon trying to expand it within the rsync command, I noticed the flag is escaped. From the bash debugger, I can see my command of rsync $excludes becomes rsync '--exclude=foo,bar,baz', rather than the expected rsync --exclude=foo,bar,baz. Doing some testing, I found that any string containing =, , or will get wrapped in single quotes when expanded. Short of using eval on a constructed string, is there a way around this?







share|improve this question



















  • Do you really want it to expand to --exclude=foo,bar,baz or are you trying to have brace expansion expand it to: --exclude=foo --exclude=bar --exclude=baz?
    – Jesse_b
    Jul 4 at 14:35










  • The former is the goal.
    – ollien
    Jul 4 at 14:35










  • I also think that isn't the right syntax. Should be --exclude foo. I don't think rsync takes braces either though.
    – Jesse_b
    Jul 4 at 14:36











  • Using the equals sign is the correct syntax.
    – ollien
    Jul 4 at 14:36










  • From the man page, --exclude=PATTERN exclude files matching PATTERN
    – ollien
    Jul 4 at 14:38














up vote
1
down vote

favorite
1












I'm writing a script that uses rsync and excludes files based on certain settings. I end up with an exclude flag that has a format of --exclude=foo, bar, baz.



However, upon trying to expand it within the rsync command, I noticed the flag is escaped. From the bash debugger, I can see my command of rsync $excludes becomes rsync '--exclude=foo,bar,baz', rather than the expected rsync --exclude=foo,bar,baz. Doing some testing, I found that any string containing =, , or will get wrapped in single quotes when expanded. Short of using eval on a constructed string, is there a way around this?







share|improve this question



















  • Do you really want it to expand to --exclude=foo,bar,baz or are you trying to have brace expansion expand it to: --exclude=foo --exclude=bar --exclude=baz?
    – Jesse_b
    Jul 4 at 14:35










  • The former is the goal.
    – ollien
    Jul 4 at 14:35










  • I also think that isn't the right syntax. Should be --exclude foo. I don't think rsync takes braces either though.
    – Jesse_b
    Jul 4 at 14:36











  • Using the equals sign is the correct syntax.
    – ollien
    Jul 4 at 14:36










  • From the man page, --exclude=PATTERN exclude files matching PATTERN
    – ollien
    Jul 4 at 14:38












up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





I'm writing a script that uses rsync and excludes files based on certain settings. I end up with an exclude flag that has a format of --exclude=foo, bar, baz.



However, upon trying to expand it within the rsync command, I noticed the flag is escaped. From the bash debugger, I can see my command of rsync $excludes becomes rsync '--exclude=foo,bar,baz', rather than the expected rsync --exclude=foo,bar,baz. Doing some testing, I found that any string containing =, , or will get wrapped in single quotes when expanded. Short of using eval on a constructed string, is there a way around this?







share|improve this question











I'm writing a script that uses rsync and excludes files based on certain settings. I end up with an exclude flag that has a format of --exclude=foo, bar, baz.



However, upon trying to expand it within the rsync command, I noticed the flag is escaped. From the bash debugger, I can see my command of rsync $excludes becomes rsync '--exclude=foo,bar,baz', rather than the expected rsync --exclude=foo,bar,baz. Doing some testing, I found that any string containing =, , or will get wrapped in single quotes when expanded. Short of using eval on a constructed string, is there a way around this?









share|improve this question










share|improve this question




share|improve this question









asked Jul 4 at 14:23









ollien

1045




1045











  • Do you really want it to expand to --exclude=foo,bar,baz or are you trying to have brace expansion expand it to: --exclude=foo --exclude=bar --exclude=baz?
    – Jesse_b
    Jul 4 at 14:35










  • The former is the goal.
    – ollien
    Jul 4 at 14:35










  • I also think that isn't the right syntax. Should be --exclude foo. I don't think rsync takes braces either though.
    – Jesse_b
    Jul 4 at 14:36











  • Using the equals sign is the correct syntax.
    – ollien
    Jul 4 at 14:36










  • From the man page, --exclude=PATTERN exclude files matching PATTERN
    – ollien
    Jul 4 at 14:38
















  • Do you really want it to expand to --exclude=foo,bar,baz or are you trying to have brace expansion expand it to: --exclude=foo --exclude=bar --exclude=baz?
    – Jesse_b
    Jul 4 at 14:35










  • The former is the goal.
    – ollien
    Jul 4 at 14:35










  • I also think that isn't the right syntax. Should be --exclude foo. I don't think rsync takes braces either though.
    – Jesse_b
    Jul 4 at 14:36











  • Using the equals sign is the correct syntax.
    – ollien
    Jul 4 at 14:36










  • From the man page, --exclude=PATTERN exclude files matching PATTERN
    – ollien
    Jul 4 at 14:38















Do you really want it to expand to --exclude=foo,bar,baz or are you trying to have brace expansion expand it to: --exclude=foo --exclude=bar --exclude=baz?
– Jesse_b
Jul 4 at 14:35




Do you really want it to expand to --exclude=foo,bar,baz or are you trying to have brace expansion expand it to: --exclude=foo --exclude=bar --exclude=baz?
– Jesse_b
Jul 4 at 14:35












The former is the goal.
– ollien
Jul 4 at 14:35




The former is the goal.
– ollien
Jul 4 at 14:35












I also think that isn't the right syntax. Should be --exclude foo. I don't think rsync takes braces either though.
– Jesse_b
Jul 4 at 14:36





I also think that isn't the right syntax. Should be --exclude foo. I don't think rsync takes braces either though.
– Jesse_b
Jul 4 at 14:36













Using the equals sign is the correct syntax.
– ollien
Jul 4 at 14:36




Using the equals sign is the correct syntax.
– ollien
Jul 4 at 14:36












From the man page, --exclude=PATTERN exclude files matching PATTERN
– ollien
Jul 4 at 14:38




From the man page, --exclude=PATTERN exclude files matching PATTERN
– ollien
Jul 4 at 14:38










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










Brace expansion won't occur when quoted. You should also store arguments in an array rather than a variable (when possible).



I think the following should work for you:



excludes=( $(--exclude=foo,bar,baz) )
rsync "$excludes[@]"





share|improve this answer























  • This does not work. This expands to rsync '--exclude=foo' '--exclude=bar' '--exclude=baz'
    – ollien
    Jul 4 at 15:03










  • @ollien: That is what you need. --exclude=foo,bar,baz would be a syntax error to rsync
    – Jesse_b
    Jul 4 at 15:07











  • rsync does not recognize the flag when it is prepended by a single quote.
    – ollien
    Jul 4 at 15:09










  • The single quote doesn't actually go to rsync, it will be consumed by the shell
    – Jesse_b
    Jul 4 at 15:10






  • 1




    You definitely do not need the echo in there. excl=( --exclude=a,b,c ) works in bash.
    – Kusalananda
    Jul 8 at 12:24

















up vote
1
down vote














From the bash debugger, ... any string containing =, , or will get wrapped in single quotes when expanded.




If you mean the xtrace output (set -x), it does indeed like to display arguments in single quotes when they contain special characters. The output is in a format that would be usable as input to the shell.



That doesn't mean the quotes are part of the string, though.



Compare:



$ echo abc def ghi
+ echo abc 'def ghi'
abc def ghi

$ echo abc 'def ghi'
+ echo abc ''''def ghi''''
abc 'def ghi'


Though as far as I can see, it doesn't bother to quote strings containing the equal sign.






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%2f453442%2fexpanding-a-variable-containing-an-equals-sign-and-curly-braces%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










    Brace expansion won't occur when quoted. You should also store arguments in an array rather than a variable (when possible).



    I think the following should work for you:



    excludes=( $(--exclude=foo,bar,baz) )
    rsync "$excludes[@]"





    share|improve this answer























    • This does not work. This expands to rsync '--exclude=foo' '--exclude=bar' '--exclude=baz'
      – ollien
      Jul 4 at 15:03










    • @ollien: That is what you need. --exclude=foo,bar,baz would be a syntax error to rsync
      – Jesse_b
      Jul 4 at 15:07











    • rsync does not recognize the flag when it is prepended by a single quote.
      – ollien
      Jul 4 at 15:09










    • The single quote doesn't actually go to rsync, it will be consumed by the shell
      – Jesse_b
      Jul 4 at 15:10






    • 1




      You definitely do not need the echo in there. excl=( --exclude=a,b,c ) works in bash.
      – Kusalananda
      Jul 8 at 12:24














    up vote
    2
    down vote



    accepted










    Brace expansion won't occur when quoted. You should also store arguments in an array rather than a variable (when possible).



    I think the following should work for you:



    excludes=( $(--exclude=foo,bar,baz) )
    rsync "$excludes[@]"





    share|improve this answer























    • This does not work. This expands to rsync '--exclude=foo' '--exclude=bar' '--exclude=baz'
      – ollien
      Jul 4 at 15:03










    • @ollien: That is what you need. --exclude=foo,bar,baz would be a syntax error to rsync
      – Jesse_b
      Jul 4 at 15:07











    • rsync does not recognize the flag when it is prepended by a single quote.
      – ollien
      Jul 4 at 15:09










    • The single quote doesn't actually go to rsync, it will be consumed by the shell
      – Jesse_b
      Jul 4 at 15:10






    • 1




      You definitely do not need the echo in there. excl=( --exclude=a,b,c ) works in bash.
      – Kusalananda
      Jul 8 at 12:24












    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    Brace expansion won't occur when quoted. You should also store arguments in an array rather than a variable (when possible).



    I think the following should work for you:



    excludes=( $(--exclude=foo,bar,baz) )
    rsync "$excludes[@]"





    share|improve this answer















    Brace expansion won't occur when quoted. You should also store arguments in an array rather than a variable (when possible).



    I think the following should work for you:



    excludes=( $(--exclude=foo,bar,baz) )
    rsync "$excludes[@]"






    share|improve this answer















    share|improve this answer



    share|improve this answer








    edited Jul 8 at 13:33


























    answered Jul 4 at 14:46









    Jesse_b

    10.1k22658




    10.1k22658











    • This does not work. This expands to rsync '--exclude=foo' '--exclude=bar' '--exclude=baz'
      – ollien
      Jul 4 at 15:03










    • @ollien: That is what you need. --exclude=foo,bar,baz would be a syntax error to rsync
      – Jesse_b
      Jul 4 at 15:07











    • rsync does not recognize the flag when it is prepended by a single quote.
      – ollien
      Jul 4 at 15:09










    • The single quote doesn't actually go to rsync, it will be consumed by the shell
      – Jesse_b
      Jul 4 at 15:10






    • 1




      You definitely do not need the echo in there. excl=( --exclude=a,b,c ) works in bash.
      – Kusalananda
      Jul 8 at 12:24
















    • This does not work. This expands to rsync '--exclude=foo' '--exclude=bar' '--exclude=baz'
      – ollien
      Jul 4 at 15:03










    • @ollien: That is what you need. --exclude=foo,bar,baz would be a syntax error to rsync
      – Jesse_b
      Jul 4 at 15:07











    • rsync does not recognize the flag when it is prepended by a single quote.
      – ollien
      Jul 4 at 15:09










    • The single quote doesn't actually go to rsync, it will be consumed by the shell
      – Jesse_b
      Jul 4 at 15:10






    • 1




      You definitely do not need the echo in there. excl=( --exclude=a,b,c ) works in bash.
      – Kusalananda
      Jul 8 at 12:24















    This does not work. This expands to rsync '--exclude=foo' '--exclude=bar' '--exclude=baz'
    – ollien
    Jul 4 at 15:03




    This does not work. This expands to rsync '--exclude=foo' '--exclude=bar' '--exclude=baz'
    – ollien
    Jul 4 at 15:03












    @ollien: That is what you need. --exclude=foo,bar,baz would be a syntax error to rsync
    – Jesse_b
    Jul 4 at 15:07





    @ollien: That is what you need. --exclude=foo,bar,baz would be a syntax error to rsync
    – Jesse_b
    Jul 4 at 15:07













    rsync does not recognize the flag when it is prepended by a single quote.
    – ollien
    Jul 4 at 15:09




    rsync does not recognize the flag when it is prepended by a single quote.
    – ollien
    Jul 4 at 15:09












    The single quote doesn't actually go to rsync, it will be consumed by the shell
    – Jesse_b
    Jul 4 at 15:10




    The single quote doesn't actually go to rsync, it will be consumed by the shell
    – Jesse_b
    Jul 4 at 15:10




    1




    1




    You definitely do not need the echo in there. excl=( --exclude=a,b,c ) works in bash.
    – Kusalananda
    Jul 8 at 12:24




    You definitely do not need the echo in there. excl=( --exclude=a,b,c ) works in bash.
    – Kusalananda
    Jul 8 at 12:24












    up vote
    1
    down vote














    From the bash debugger, ... any string containing =, , or will get wrapped in single quotes when expanded.




    If you mean the xtrace output (set -x), it does indeed like to display arguments in single quotes when they contain special characters. The output is in a format that would be usable as input to the shell.



    That doesn't mean the quotes are part of the string, though.



    Compare:



    $ echo abc def ghi
    + echo abc 'def ghi'
    abc def ghi

    $ echo abc 'def ghi'
    + echo abc ''''def ghi''''
    abc 'def ghi'


    Though as far as I can see, it doesn't bother to quote strings containing the equal sign.






    share|improve this answer

























      up vote
      1
      down vote














      From the bash debugger, ... any string containing =, , or will get wrapped in single quotes when expanded.




      If you mean the xtrace output (set -x), it does indeed like to display arguments in single quotes when they contain special characters. The output is in a format that would be usable as input to the shell.



      That doesn't mean the quotes are part of the string, though.



      Compare:



      $ echo abc def ghi
      + echo abc 'def ghi'
      abc def ghi

      $ echo abc 'def ghi'
      + echo abc ''''def ghi''''
      abc 'def ghi'


      Though as far as I can see, it doesn't bother to quote strings containing the equal sign.






      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote










        From the bash debugger, ... any string containing =, , or will get wrapped in single quotes when expanded.




        If you mean the xtrace output (set -x), it does indeed like to display arguments in single quotes when they contain special characters. The output is in a format that would be usable as input to the shell.



        That doesn't mean the quotes are part of the string, though.



        Compare:



        $ echo abc def ghi
        + echo abc 'def ghi'
        abc def ghi

        $ echo abc 'def ghi'
        + echo abc ''''def ghi''''
        abc 'def ghi'


        Though as far as I can see, it doesn't bother to quote strings containing the equal sign.






        share|improve this answer














        From the bash debugger, ... any string containing =, , or will get wrapped in single quotes when expanded.




        If you mean the xtrace output (set -x), it does indeed like to display arguments in single quotes when they contain special characters. The output is in a format that would be usable as input to the shell.



        That doesn't mean the quotes are part of the string, though.



        Compare:



        $ echo abc def ghi
        + echo abc 'def ghi'
        abc def ghi

        $ echo abc 'def ghi'
        + echo abc ''''def ghi''''
        abc 'def ghi'


        Though as far as I can see, it doesn't bother to quote strings containing the equal sign.







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jul 4 at 17:25









        ilkkachu

        47.3k668130




        47.3k668130






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f453442%2fexpanding-a-variable-containing-an-equals-sign-and-curly-braces%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