0

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?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
user3504970
  • 117
  • 1
  • 7

2 Answers2

2

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.

Costi Ciudatu
  • 37,042
  • 7
  • 56
  • 92
0

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

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631