I have come across this operator =~
and couldn't figure it out on what it does. Could someone with bash knowledge kindly help me out?

- 349,597
- 67
- 533
- 578

- 117
- 1
- 7
-
In what context did you find that operator? – smac89 Nov 11 '14 at 00:47
-
http://stackoverflow.com/questions/218156/bash-regex-with-quotes – AGS Nov 11 '14 at 00:48
-
1Have you tried searching for it in the [bash documentation](http://linux.die.net/man/1/bash)? – Barmar Nov 11 '14 at 00:51
2 Answers
man bash
/=~
An additional binary operator, =~, is available, with the same precedence as == and !=. When it is used, the string to the right of the operator is considered an extended regular expression and matched accordingly (as in regex(3)). The return value is 0 if the string matches the pattern, and 1 otherwise. If the regular expression is syntactically incorrect, the conditional expression's return value is 2. If the shell option nocasematch is enabled, the match is performed without regard to the case of alphabetic characters. Substrings matched by parenthesized subexpressions within the regular expression are saved in the array variable BASH_REMATCH. The element of BASH_REMATCH with index 0 is the portion of the string matching the entire regular expression. The element of BASH_REMATCH with index n is the portion of the string matching the nth parenthesized subexpression.

- 37,042
- 7
- 56
- 92
In ~
is saved your home directory (for example: /home/username
) and =
is assignment operator.
If you run this code in bash:
x=~ # variable x will hold string value /home/your_username
echo $x # print variable x via echo command
it will print something like: /home/your_username

- 254,901
- 44
- 429
- 631

- 29
- 3
-
2True, but not at all likely to be what the OP is asking about. Bash does have an `=~` operator; see the other answer. – Keith Thompson Nov 11 '14 at 01:19