Pass a string or array as arguments in bash
Clash 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?
bash shell-script shell grep
add a comment |Â
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?
bash shell-script shell grep
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 (unlessset -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
add a comment |Â
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?
bash shell-script shell grep
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?
bash shell-script shell grep
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 (unlessset -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
add a comment |Â
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 (unlessset -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
add a comment |Â
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[@]" ...
add a comment |Â
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[@]" ...
add a comment |Â
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[@]" ...
add a comment |Â
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[@]" ...
Array works, but you need to store the options and values as separate elements:
excludes=(-e "regex1" -e "regex2")
grep -v "$excludes[@]" ...
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
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%2f409290%2fpass-a-string-or-array-as-arguments-in-bash%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
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