Using a variable as a case condition in zsh
Clash Royale CLAN TAG#URR8PPP
up vote
5
down vote
favorite
My question is the zsh equivalent of the question asked here: How can I use a variable as a case condition? I would like to use a variable for the condition of a case statement in zsh. For example:
input="foo"
pattern="(foo|bar)"
case $input in
$pattern)
echo "you sent foo or bar"
;;
*)
echo "foo or bar was not sent"
;;
esac
I would like to use the strings foo
or bar
and have the above code execute the pattern
case condition.
shell-script zsh quoting wildcards case
add a comment |Â
up vote
5
down vote
favorite
My question is the zsh equivalent of the question asked here: How can I use a variable as a case condition? I would like to use a variable for the condition of a case statement in zsh. For example:
input="foo"
pattern="(foo|bar)"
case $input in
$pattern)
echo "you sent foo or bar"
;;
*)
echo "foo or bar was not sent"
;;
esac
I would like to use the strings foo
or bar
and have the above code execute the pattern
case condition.
shell-script zsh quoting wildcards case
add a comment |Â
up vote
5
down vote
favorite
up vote
5
down vote
favorite
My question is the zsh equivalent of the question asked here: How can I use a variable as a case condition? I would like to use a variable for the condition of a case statement in zsh. For example:
input="foo"
pattern="(foo|bar)"
case $input in
$pattern)
echo "you sent foo or bar"
;;
*)
echo "foo or bar was not sent"
;;
esac
I would like to use the strings foo
or bar
and have the above code execute the pattern
case condition.
shell-script zsh quoting wildcards case
My question is the zsh equivalent of the question asked here: How can I use a variable as a case condition? I would like to use a variable for the condition of a case statement in zsh. For example:
input="foo"
pattern="(foo|bar)"
case $input in
$pattern)
echo "you sent foo or bar"
;;
*)
echo "foo or bar was not sent"
;;
esac
I would like to use the strings foo
or bar
and have the above code execute the pattern
case condition.
shell-script zsh quoting wildcards case
edited May 30 at 21:36
Gilles
503k1179951521
503k1179951521
asked May 28 at 0:24
Smashgen
984
984
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
6
down vote
accepted
With this code saved to the file first
,
pattern=fo*
input=foo
case $input in
$pattern)
print T
;;
fo*)
print NIL
;;
esac
under -x
we may observe that the variable appears as a quoted value while the raw expression does not:
% zsh -x first
+first:1> pattern='fo*'
+first:2> input=foo
+first:3> case foo (fo*)
+first:3> case foo (fo*)
+first:8> print NIL
NIL
That is, the variable is being treated as a literal string. If one spends enough time in zshexpn(1)
one might be aware of the glob substitution flag
$~spec
Turn on the GLOB_SUBST option for the evaluation of spec; if the
`~' is doubled, turn it off. When this option is set, the
string resulting from the expansion will be interpreted as a
pattern anywhere that is possible,
so if we modify $pattern
to use that
pattern=fo*
input=foo
case $input in
$~pattern) # !
print T
;;
fo*)
print NIL
;;
esac
we see instead
% zsh -x second
+second:1> pattern='fo*'
+second:2> input=foo
+second:3> case foo (fo*)
+second:5> print T
T
for your case the pattern must be quoted:
pattern='(foo|bar)'
input=foo
case $input in
$~pattern)
print T
;;
*)
print NIL
;;
esac
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
With this code saved to the file first
,
pattern=fo*
input=foo
case $input in
$pattern)
print T
;;
fo*)
print NIL
;;
esac
under -x
we may observe that the variable appears as a quoted value while the raw expression does not:
% zsh -x first
+first:1> pattern='fo*'
+first:2> input=foo
+first:3> case foo (fo*)
+first:3> case foo (fo*)
+first:8> print NIL
NIL
That is, the variable is being treated as a literal string. If one spends enough time in zshexpn(1)
one might be aware of the glob substitution flag
$~spec
Turn on the GLOB_SUBST option for the evaluation of spec; if the
`~' is doubled, turn it off. When this option is set, the
string resulting from the expansion will be interpreted as a
pattern anywhere that is possible,
so if we modify $pattern
to use that
pattern=fo*
input=foo
case $input in
$~pattern) # !
print T
;;
fo*)
print NIL
;;
esac
we see instead
% zsh -x second
+second:1> pattern='fo*'
+second:2> input=foo
+second:3> case foo (fo*)
+second:5> print T
T
for your case the pattern must be quoted:
pattern='(foo|bar)'
input=foo
case $input in
$~pattern)
print T
;;
*)
print NIL
;;
esac
add a comment |Â
up vote
6
down vote
accepted
With this code saved to the file first
,
pattern=fo*
input=foo
case $input in
$pattern)
print T
;;
fo*)
print NIL
;;
esac
under -x
we may observe that the variable appears as a quoted value while the raw expression does not:
% zsh -x first
+first:1> pattern='fo*'
+first:2> input=foo
+first:3> case foo (fo*)
+first:3> case foo (fo*)
+first:8> print NIL
NIL
That is, the variable is being treated as a literal string. If one spends enough time in zshexpn(1)
one might be aware of the glob substitution flag
$~spec
Turn on the GLOB_SUBST option for the evaluation of spec; if the
`~' is doubled, turn it off. When this option is set, the
string resulting from the expansion will be interpreted as a
pattern anywhere that is possible,
so if we modify $pattern
to use that
pattern=fo*
input=foo
case $input in
$~pattern) # !
print T
;;
fo*)
print NIL
;;
esac
we see instead
% zsh -x second
+second:1> pattern='fo*'
+second:2> input=foo
+second:3> case foo (fo*)
+second:5> print T
T
for your case the pattern must be quoted:
pattern='(foo|bar)'
input=foo
case $input in
$~pattern)
print T
;;
*)
print NIL
;;
esac
add a comment |Â
up vote
6
down vote
accepted
up vote
6
down vote
accepted
With this code saved to the file first
,
pattern=fo*
input=foo
case $input in
$pattern)
print T
;;
fo*)
print NIL
;;
esac
under -x
we may observe that the variable appears as a quoted value while the raw expression does not:
% zsh -x first
+first:1> pattern='fo*'
+first:2> input=foo
+first:3> case foo (fo*)
+first:3> case foo (fo*)
+first:8> print NIL
NIL
That is, the variable is being treated as a literal string. If one spends enough time in zshexpn(1)
one might be aware of the glob substitution flag
$~spec
Turn on the GLOB_SUBST option for the evaluation of spec; if the
`~' is doubled, turn it off. When this option is set, the
string resulting from the expansion will be interpreted as a
pattern anywhere that is possible,
so if we modify $pattern
to use that
pattern=fo*
input=foo
case $input in
$~pattern) # !
print T
;;
fo*)
print NIL
;;
esac
we see instead
% zsh -x second
+second:1> pattern='fo*'
+second:2> input=foo
+second:3> case foo (fo*)
+second:5> print T
T
for your case the pattern must be quoted:
pattern='(foo|bar)'
input=foo
case $input in
$~pattern)
print T
;;
*)
print NIL
;;
esac
With this code saved to the file first
,
pattern=fo*
input=foo
case $input in
$pattern)
print T
;;
fo*)
print NIL
;;
esac
under -x
we may observe that the variable appears as a quoted value while the raw expression does not:
% zsh -x first
+first:1> pattern='fo*'
+first:2> input=foo
+first:3> case foo (fo*)
+first:3> case foo (fo*)
+first:8> print NIL
NIL
That is, the variable is being treated as a literal string. If one spends enough time in zshexpn(1)
one might be aware of the glob substitution flag
$~spec
Turn on the GLOB_SUBST option for the evaluation of spec; if the
`~' is doubled, turn it off. When this option is set, the
string resulting from the expansion will be interpreted as a
pattern anywhere that is possible,
so if we modify $pattern
to use that
pattern=fo*
input=foo
case $input in
$~pattern) # !
print T
;;
fo*)
print NIL
;;
esac
we see instead
% zsh -x second
+second:1> pattern='fo*'
+second:2> input=foo
+second:3> case foo (fo*)
+second:5> print T
T
for your case the pattern must be quoted:
pattern='(foo|bar)'
input=foo
case $input in
$~pattern)
print T
;;
*)
print NIL
;;
esac
answered May 28 at 2:41
thrig
21.9k12751
21.9k12751
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%2f446374%2fusing-a-variable-as-a-case-condition-in-zsh%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