Pass a string or array as arguments in bash

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











up vote
1
down vote

favorite












I have excludes as a variable, where it's meant to be a list of regexes to pass to grep:



$ echo $excludes
-e re_1 -e re_2 -e re_3...


I'd like to be able to do something like



$ my | pipeline | grep -v "$excludes"


but this doesn't work.



I also tried using an array as in grep -v "$excludes[@]" where each array member is "-e blah". This didn't work either.



How can I pass arguments in a programmatic way like this?







share|improve this question
















  • 2




    I think this may be one of the cases where you should not quote your variable.
    – Jesse_b
    Dec 6 '17 at 19:50






  • 1




    @Jesse_b, only works if the regexes don't contain whitespace, and they'd get used as globs too (unless set -f), which isn't likely to be good since regexes often contain the very same characters that are special in globs...
    – ilkkachu
    Dec 6 '17 at 20:24










  • Where do your regexes come from? A space-separated string like that seems fiddly if you ever happen to need regexes that contain spaces...
    – ilkkachu
    Dec 6 '17 at 20:31










  • I control the source of the regexes, so that's not an issue in this case. choroba's answer was what I needed.
    – Jon Cohen
    Dec 6 '17 at 21:25














up vote
1
down vote

favorite












I have excludes as a variable, where it's meant to be a list of regexes to pass to grep:



$ echo $excludes
-e re_1 -e re_2 -e re_3...


I'd like to be able to do something like



$ my | pipeline | grep -v "$excludes"


but this doesn't work.



I also tried using an array as in grep -v "$excludes[@]" where each array member is "-e blah". This didn't work either.



How can I pass arguments in a programmatic way like this?







share|improve this question
















  • 2




    I think this may be one of the cases where you should not quote your variable.
    – Jesse_b
    Dec 6 '17 at 19:50






  • 1




    @Jesse_b, only works if the regexes don't contain whitespace, and they'd get used as globs too (unless set -f), which isn't likely to be good since regexes often contain the very same characters that are special in globs...
    – ilkkachu
    Dec 6 '17 at 20:24










  • Where do your regexes come from? A space-separated string like that seems fiddly if you ever happen to need regexes that contain spaces...
    – ilkkachu
    Dec 6 '17 at 20:31










  • I control the source of the regexes, so that's not an issue in this case. choroba's answer was what I needed.
    – Jon Cohen
    Dec 6 '17 at 21:25












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have excludes as a variable, where it's meant to be a list of regexes to pass to grep:



$ echo $excludes
-e re_1 -e re_2 -e re_3...


I'd like to be able to do something like



$ my | pipeline | grep -v "$excludes"


but this doesn't work.



I also tried using an array as in grep -v "$excludes[@]" where each array member is "-e blah". This didn't work either.



How can I pass arguments in a programmatic way like this?







share|improve this question












I have excludes as a variable, where it's meant to be a list of regexes to pass to grep:



$ echo $excludes
-e re_1 -e re_2 -e re_3...


I'd like to be able to do something like



$ my | pipeline | grep -v "$excludes"


but this doesn't work.



I also tried using an array as in grep -v "$excludes[@]" where each array member is "-e blah". This didn't work either.



How can I pass arguments in a programmatic way like this?









share|improve this question











share|improve this question




share|improve this question










asked Dec 6 '17 at 19:49









Jon Cohen

82




82







  • 2




    I think this may be one of the cases where you should not quote your variable.
    – Jesse_b
    Dec 6 '17 at 19:50






  • 1




    @Jesse_b, only works if the regexes don't contain whitespace, and they'd get used as globs too (unless set -f), which isn't likely to be good since regexes often contain the very same characters that are special in globs...
    – ilkkachu
    Dec 6 '17 at 20:24










  • Where do your regexes come from? A space-separated string like that seems fiddly if you ever happen to need regexes that contain spaces...
    – ilkkachu
    Dec 6 '17 at 20:31










  • I control the source of the regexes, so that's not an issue in this case. choroba's answer was what I needed.
    – Jon Cohen
    Dec 6 '17 at 21:25












  • 2




    I think this may be one of the cases where you should not quote your variable.
    – Jesse_b
    Dec 6 '17 at 19:50






  • 1




    @Jesse_b, only works if the regexes don't contain whitespace, and they'd get used as globs too (unless set -f), which isn't likely to be good since regexes often contain the very same characters that are special in globs...
    – ilkkachu
    Dec 6 '17 at 20:24










  • Where do your regexes come from? A space-separated string like that seems fiddly if you ever happen to need regexes that contain spaces...
    – ilkkachu
    Dec 6 '17 at 20:31










  • I control the source of the regexes, so that's not an issue in this case. choroba's answer was what I needed.
    – Jon Cohen
    Dec 6 '17 at 21:25







2




2




I think this may be one of the cases where you should not quote your variable.
– Jesse_b
Dec 6 '17 at 19:50




I think this may be one of the cases where you should not quote your variable.
– Jesse_b
Dec 6 '17 at 19:50




1




1




@Jesse_b, only works if the regexes don't contain whitespace, and they'd get used as globs too (unless set -f), which isn't likely to be good since regexes often contain the very same characters that are special in globs...
– ilkkachu
Dec 6 '17 at 20:24




@Jesse_b, only works if the regexes don't contain whitespace, and they'd get used as globs too (unless set -f), which isn't likely to be good since regexes often contain the very same characters that are special in globs...
– ilkkachu
Dec 6 '17 at 20:24












Where do your regexes come from? A space-separated string like that seems fiddly if you ever happen to need regexes that contain spaces...
– ilkkachu
Dec 6 '17 at 20:31




Where do your regexes come from? A space-separated string like that seems fiddly if you ever happen to need regexes that contain spaces...
– ilkkachu
Dec 6 '17 at 20:31












I control the source of the regexes, so that's not an issue in this case. choroba's answer was what I needed.
– Jon Cohen
Dec 6 '17 at 21:25




I control the source of the regexes, so that's not an issue in this case. choroba's answer was what I needed.
– Jon Cohen
Dec 6 '17 at 21:25










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Array works, but you need to store the options and values as separate elements:



excludes=(-e "regex1" -e "regex2")
grep -v "$excludes[@]" ...





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%2f409290%2fpass-a-string-or-array-as-arguments-in-bash%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
    2
    down vote



    accepted










    Array works, but you need to store the options and values as separate elements:



    excludes=(-e "regex1" -e "regex2")
    grep -v "$excludes[@]" ...





    share|improve this answer


























      up vote
      2
      down vote



      accepted










      Array works, but you need to store the options and values as separate elements:



      excludes=(-e "regex1" -e "regex2")
      grep -v "$excludes[@]" ...





      share|improve this answer
























        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        Array works, but you need to store the options and values as separate elements:



        excludes=(-e "regex1" -e "regex2")
        grep -v "$excludes[@]" ...





        share|improve this answer














        Array works, but you need to store the options and values as separate elements:



        excludes=(-e "regex1" -e "regex2")
        grep -v "$excludes[@]" ...






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 6 '17 at 20:40









        glenn jackman

        46.8k265103




        46.8k265103










        answered Dec 6 '17 at 19:52









        choroba

        24.4k34168




        24.4k34168



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f409290%2fpass-a-string-or-array-as-arguments-in-bash%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