Condition regex error

The name of the pictureThe name of the pictureThe name of the pictureClash 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






share|improve this question




















  • 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




    @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














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






share|improve this question




















  • 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




    @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












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






share|improve this question












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








share|improve this question











share|improve this question




share|improve this question










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 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




    @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
















  • 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




    @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















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










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





share|improve this answer






















  • @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











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%2f399764%2fcondition-regex-error%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
24
down vote



accepted










Bash's regex matching works only within double square brackets [[ ... ]]:



string=123456
if [[ "$string" =~ 123456 ]]; then echo 123; fi
123





share|improve this answer






















  • @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















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





share|improve this answer






















  • @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













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





share|improve this answer














Bash's regex matching works only within double square brackets [[ ... ]]:



string=123456
if [[ "$string" =~ 123456 ]]; then echo 123; fi
123






share|improve this answer














share|improve this answer



share|improve this answer








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

















  • @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


















 

draft saved


draft discarded















































 


draft saved


draft discarded














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













































































Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)