Meaning of BASH operators â^â, â|â and â$â in condition ^(n|N)$

Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
In my script, I am trying to make "Try again? [y/N]" condition...
if [[ "$response" =~ ^(n|no)$ ]]; then do something...,
but I don't understand meaning of symbols "^" and "$" used here. I tried just simple [[ if $response =~ (n|no) ]] and it worked, but if I typed something like "nein" it still worked... I took pipe (|) as "OR" operator, but it's probably also wrong.
Could somebody explain me the meaning of whole condition? It works, but I would like to know meaning too... Thank you very much.
bash
add a comment |Â
up vote
2
down vote
favorite
In my script, I am trying to make "Try again? [y/N]" condition...
if [[ "$response" =~ ^(n|no)$ ]]; then do something...,
but I don't understand meaning of symbols "^" and "$" used here. I tried just simple [[ if $response =~ (n|no) ]] and it worked, but if I typed something like "nein" it still worked... I took pipe (|) as "OR" operator, but it's probably also wrong.
Could somebody explain me the meaning of whole condition? It works, but I would like to know meaning too... Thank you very much.
bash
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
In my script, I am trying to make "Try again? [y/N]" condition...
if [[ "$response" =~ ^(n|no)$ ]]; then do something...,
but I don't understand meaning of symbols "^" and "$" used here. I tried just simple [[ if $response =~ (n|no) ]] and it worked, but if I typed something like "nein" it still worked... I took pipe (|) as "OR" operator, but it's probably also wrong.
Could somebody explain me the meaning of whole condition? It works, but I would like to know meaning too... Thank you very much.
bash
In my script, I am trying to make "Try again? [y/N]" condition...
if [[ "$response" =~ ^(n|no)$ ]]; then do something...,
but I don't understand meaning of symbols "^" and "$" used here. I tried just simple [[ if $response =~ (n|no) ]] and it worked, but if I typed something like "nein" it still worked... I took pipe (|) as "OR" operator, but it's probably also wrong.
Could somebody explain me the meaning of whole condition? It works, but I would like to know meaning too... Thank you very much.
bash
edited Mar 24 at 23:44
Jesse_b
10.4k22658
10.4k22658
asked Mar 24 at 23:43
Michal
141
141
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
8
down vote
These are regular expression special characters.
^ is an anchor for the start of string (So nothing can be before the match)
$ is an anchor for the end of string (so nothing can be after the match)
| is OR as you suspected
The =~ tells the bash extended test that the string on the right is an extended regular expression and will exit with 0 for match and 1 for anything else.
[[ if $response =~ (n|no) ]] is flawed as the if should be outside the brackets, but assuming that is a typo in your question it will succeed if response contains an n at all. You likely want the start and end anchors so that only n or no will match.
3
Probably also worth mentioning that =~ is what causes the right hand side to be interpreted as a regex which causes âÂÂneinâ to make the test true.
â neal
Mar 25 at 4:52
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
These are regular expression special characters.
^ is an anchor for the start of string (So nothing can be before the match)
$ is an anchor for the end of string (so nothing can be after the match)
| is OR as you suspected
The =~ tells the bash extended test that the string on the right is an extended regular expression and will exit with 0 for match and 1 for anything else.
[[ if $response =~ (n|no) ]] is flawed as the if should be outside the brackets, but assuming that is a typo in your question it will succeed if response contains an n at all. You likely want the start and end anchors so that only n or no will match.
3
Probably also worth mentioning that =~ is what causes the right hand side to be interpreted as a regex which causes âÂÂneinâ to make the test true.
â neal
Mar 25 at 4:52
add a comment |Â
up vote
8
down vote
These are regular expression special characters.
^ is an anchor for the start of string (So nothing can be before the match)
$ is an anchor for the end of string (so nothing can be after the match)
| is OR as you suspected
The =~ tells the bash extended test that the string on the right is an extended regular expression and will exit with 0 for match and 1 for anything else.
[[ if $response =~ (n|no) ]] is flawed as the if should be outside the brackets, but assuming that is a typo in your question it will succeed if response contains an n at all. You likely want the start and end anchors so that only n or no will match.
3
Probably also worth mentioning that =~ is what causes the right hand side to be interpreted as a regex which causes âÂÂneinâ to make the test true.
â neal
Mar 25 at 4:52
add a comment |Â
up vote
8
down vote
up vote
8
down vote
These are regular expression special characters.
^ is an anchor for the start of string (So nothing can be before the match)
$ is an anchor for the end of string (so nothing can be after the match)
| is OR as you suspected
The =~ tells the bash extended test that the string on the right is an extended regular expression and will exit with 0 for match and 1 for anything else.
[[ if $response =~ (n|no) ]] is flawed as the if should be outside the brackets, but assuming that is a typo in your question it will succeed if response contains an n at all. You likely want the start and end anchors so that only n or no will match.
These are regular expression special characters.
^ is an anchor for the start of string (So nothing can be before the match)
$ is an anchor for the end of string (so nothing can be after the match)
| is OR as you suspected
The =~ tells the bash extended test that the string on the right is an extended regular expression and will exit with 0 for match and 1 for anything else.
[[ if $response =~ (n|no) ]] is flawed as the if should be outside the brackets, but assuming that is a typo in your question it will succeed if response contains an n at all. You likely want the start and end anchors so that only n or no will match.
edited Mar 25 at 11:03
answered Mar 24 at 23:46
Jesse_b
10.4k22658
10.4k22658
3
Probably also worth mentioning that =~ is what causes the right hand side to be interpreted as a regex which causes âÂÂneinâ to make the test true.
â neal
Mar 25 at 4:52
add a comment |Â
3
Probably also worth mentioning that =~ is what causes the right hand side to be interpreted as a regex which causes âÂÂneinâ to make the test true.
â neal
Mar 25 at 4:52
3
3
Probably also worth mentioning that =~ is what causes the right hand side to be interpreted as a regex which causes âÂÂneinâ to make the test true.
â neal
Mar 25 at 4:52
Probably also worth mentioning that =~ is what causes the right hand side to be interpreted as a regex which causes âÂÂneinâ to make the test true.
â neal
Mar 25 at 4:52
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%2f433342%2fmeaning-of-bash-operators-and-in-condition-nn%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