Condition regex error

Clash Royale CLAN TAG#URR8PPP
up vote
12
down vote
favorite
2
string=123456
if [ $string == 123456 ]; then
echo 123
fi
This works fine, but if I change == to =~ I get this error:
./test: line 3: [: =~: binary operator expected
bash shell-script
add a comment |Â
up vote
12
down vote
favorite
2
string=123456
if [ $string == 123456 ]; then
echo 123
fi
This works fine, but if I change == to =~ I get this error:
./test: line 3: [: =~: binary operator expected
bash shell-script
The use of=~is in this case wrong.=~compares the patter (left side of the assignment) with a regular expresionregexon the right side of the assinment. A regular expression in its simplest form is passed like'[0-9][0-9]'
â Valentin B
Oct 22 '17 at 18:42
8
@val0x00ff but123456is a valid RE
â roaima
Oct 22 '17 at 19:01
@roaima agreed, however regex is known for its engine, back-referencing, character-set, meta chars denoting starting of a string, ending of a string etc.
â Valentin B
Oct 22 '17 at 19:11
add a comment |Â
up vote
12
down vote
favorite
2
up vote
12
down vote
favorite
2
2
string=123456
if [ $string == 123456 ]; then
echo 123
fi
This works fine, but if I change == to =~ I get this error:
./test: line 3: [: =~: binary operator expected
bash shell-script
string=123456
if [ $string == 123456 ]; then
echo 123
fi
This works fine, but if I change == to =~ I get this error:
./test: line 3: [: =~: binary operator expected
bash shell-script
asked Oct 22 '17 at 18:25
Lumify
1217
1217
The use of=~is in this case wrong.=~compares the patter (left side of the assignment) with a regular expresionregexon the right side of the assinment. A regular expression in its simplest form is passed like'[0-9][0-9]'
â Valentin B
Oct 22 '17 at 18:42
8
@val0x00ff but123456is a valid RE
â roaima
Oct 22 '17 at 19:01
@roaima agreed, however regex is known for its engine, back-referencing, character-set, meta chars denoting starting of a string, ending of a string etc.
â Valentin B
Oct 22 '17 at 19:11
add a comment |Â
The use of=~is in this case wrong.=~compares the patter (left side of the assignment) with a regular expresionregexon the right side of the assinment. A regular expression in its simplest form is passed like'[0-9][0-9]'
â Valentin B
Oct 22 '17 at 18:42
8
@val0x00ff but123456is a valid RE
â roaima
Oct 22 '17 at 19:01
@roaima agreed, however regex is known for its engine, back-referencing, character-set, meta chars denoting starting of a string, ending of a string etc.
â Valentin B
Oct 22 '17 at 19:11
The use of
=~ is in this case wrong. =~ compares the patter (left side of the assignment) with a regular expresion regex on the right side of the assinment. A regular expression in its simplest form is passed like '[0-9][0-9]'â Valentin B
Oct 22 '17 at 18:42
The use of
=~ is in this case wrong. =~ compares the patter (left side of the assignment) with a regular expresion regex on the right side of the assinment. A regular expression in its simplest form is passed like '[0-9][0-9]'â Valentin B
Oct 22 '17 at 18:42
8
8
@val0x00ff but
123456 is a valid REâ roaima
Oct 22 '17 at 19:01
@val0x00ff but
123456 is a valid REâ roaima
Oct 22 '17 at 19:01
@roaima agreed, however regex is known for its engine, back-referencing, character-set, meta chars denoting starting of a string, ending of a string etc.
â Valentin B
Oct 22 '17 at 19:11
@roaima agreed, however regex is known for its engine, back-referencing, character-set, meta chars denoting starting of a string, ending of a string etc.
â Valentin B
Oct 22 '17 at 19:11
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
24
down vote
accepted
Bash's regex matching works only within double square brackets [[ ... ]]:
string=123456
if [[ "$string" =~ 123456 ]]; then echo 123; fi
123
@Kusalananda: Regarding the edit, the quotes are not required on the left hand side of a double bracket expression[[ ... ]], since no word splitting or pathname expansion occurs there. They are only needed when using single brackets[ ... ](both sides) and on the right hand of the double bracket expression.
â user000001
Oct 23 '17 at 11:21
@user000001 while you're correct, it's much better just to quote all variables by reflex unless there's a necessity not to. It's a much safer habit.
â Joe
Oct 27 '17 at 20:00
@Joe: Agreed, when in doubt, quote... I only mentioned this because it was an edit to an already correct answer. Had it been there from the first revision I would not have commented on this.
â user000001
Oct 28 '17 at 13:06
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
24
down vote
accepted
Bash's regex matching works only within double square brackets [[ ... ]]:
string=123456
if [[ "$string" =~ 123456 ]]; then echo 123; fi
123
@Kusalananda: Regarding the edit, the quotes are not required on the left hand side of a double bracket expression[[ ... ]], since no word splitting or pathname expansion occurs there. They are only needed when using single brackets[ ... ](both sides) and on the right hand of the double bracket expression.
â user000001
Oct 23 '17 at 11:21
@user000001 while you're correct, it's much better just to quote all variables by reflex unless there's a necessity not to. It's a much safer habit.
â Joe
Oct 27 '17 at 20:00
@Joe: Agreed, when in doubt, quote... I only mentioned this because it was an edit to an already correct answer. Had it been there from the first revision I would not have commented on this.
â user000001
Oct 28 '17 at 13:06
add a comment |Â
up vote
24
down vote
accepted
Bash's regex matching works only within double square brackets [[ ... ]]:
string=123456
if [[ "$string" =~ 123456 ]]; then echo 123; fi
123
@Kusalananda: Regarding the edit, the quotes are not required on the left hand side of a double bracket expression[[ ... ]], since no word splitting or pathname expansion occurs there. They are only needed when using single brackets[ ... ](both sides) and on the right hand of the double bracket expression.
â user000001
Oct 23 '17 at 11:21
@user000001 while you're correct, it's much better just to quote all variables by reflex unless there's a necessity not to. It's a much safer habit.
â Joe
Oct 27 '17 at 20:00
@Joe: Agreed, when in doubt, quote... I only mentioned this because it was an edit to an already correct answer. Had it been there from the first revision I would not have commented on this.
â user000001
Oct 28 '17 at 13:06
add a comment |Â
up vote
24
down vote
accepted
up vote
24
down vote
accepted
Bash's regex matching works only within double square brackets [[ ... ]]:
string=123456
if [[ "$string" =~ 123456 ]]; then echo 123; fi
123
Bash's regex matching works only within double square brackets [[ ... ]]:
string=123456
if [[ "$string" =~ 123456 ]]; then echo 123; fi
123
edited Oct 22 '17 at 19:54
Kusalananda
105k14209326
105k14209326
answered Oct 22 '17 at 18:30
RomanPerekhrest
22.5k12145
22.5k12145
@Kusalananda: Regarding the edit, the quotes are not required on the left hand side of a double bracket expression[[ ... ]], since no word splitting or pathname expansion occurs there. They are only needed when using single brackets[ ... ](both sides) and on the right hand of the double bracket expression.
â user000001
Oct 23 '17 at 11:21
@user000001 while you're correct, it's much better just to quote all variables by reflex unless there's a necessity not to. It's a much safer habit.
â Joe
Oct 27 '17 at 20:00
@Joe: Agreed, when in doubt, quote... I only mentioned this because it was an edit to an already correct answer. Had it been there from the first revision I would not have commented on this.
â user000001
Oct 28 '17 at 13:06
add a comment |Â
@Kusalananda: Regarding the edit, the quotes are not required on the left hand side of a double bracket expression[[ ... ]], since no word splitting or pathname expansion occurs there. They are only needed when using single brackets[ ... ](both sides) and on the right hand of the double bracket expression.
â user000001
Oct 23 '17 at 11:21
@user000001 while you're correct, it's much better just to quote all variables by reflex unless there's a necessity not to. It's a much safer habit.
â Joe
Oct 27 '17 at 20:00
@Joe: Agreed, when in doubt, quote... I only mentioned this because it was an edit to an already correct answer. Had it been there from the first revision I would not have commented on this.
â user000001
Oct 28 '17 at 13:06
@Kusalananda: Regarding the edit, the quotes are not required on the left hand side of a double bracket expression
[[ ... ]], since no word splitting or pathname expansion occurs there. They are only needed when using single brackets [ ... ] (both sides) and on the right hand of the double bracket expression.â user000001
Oct 23 '17 at 11:21
@Kusalananda: Regarding the edit, the quotes are not required on the left hand side of a double bracket expression
[[ ... ]], since no word splitting or pathname expansion occurs there. They are only needed when using single brackets [ ... ] (both sides) and on the right hand of the double bracket expression.â user000001
Oct 23 '17 at 11:21
@user000001 while you're correct, it's much better just to quote all variables by reflex unless there's a necessity not to. It's a much safer habit.
â Joe
Oct 27 '17 at 20:00
@user000001 while you're correct, it's much better just to quote all variables by reflex unless there's a necessity not to. It's a much safer habit.
â Joe
Oct 27 '17 at 20:00
@Joe: Agreed, when in doubt, quote... I only mentioned this because it was an edit to an already correct answer. Had it been there from the first revision I would not have commented on this.
â user000001
Oct 28 '17 at 13:06
@Joe: Agreed, when in doubt, quote... I only mentioned this because it was an edit to an already correct answer. Had it been there from the first revision I would not have commented on this.
â user000001
Oct 28 '17 at 13:06
add a comment |Â
Â
draft saved
draft discarded
Â
draft saved
draft discarded
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%2f399764%2fcondition-regex-error%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
The use of
=~is in this case wrong.=~compares the patter (left side of the assignment) with a regular expresionregexon the right side of the assinment. A regular expression in its simplest form is passed like'[0-9][0-9]'â Valentin B
Oct 22 '17 at 18:42
8
@val0x00ff but
123456is a valid REâ roaima
Oct 22 '17 at 19:01
@roaima agreed, however regex is known for its engine, back-referencing, character-set, meta chars denoting starting of a string, ending of a string etc.
â Valentin B
Oct 22 '17 at 19:11