Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of "=~" operator in shell script [duplicate]

I came across a shell script where the code is

for line in $LIST_ARRAY;do if [[ $LIST_ARRAY =~ $line ]] then echo "true" .... ... . 

What is the use of =~ in this case?

like image 561
cc4re Avatar asked Sep 17 '12 07:09

cc4re


People also ask

What does =~ mean in shell?

it's the Equal Tilde operator that allows the use of regex in an if statement. An additional binary operator, =~, is available, with the same precedence as == and !=

What does >> mean in scripting?

83. > is used to overwrite (“clobber”) a file and >> is used to append to a file. Thus, when you use ps aux > file , the output of ps aux will be written to file and if a file named file was already present, its contents will be overwritten.

What does 2 >> mean in Linux?

(other special file descriptors include 0 for standard input and 1 for standard output). 2> /dev/null means to redirect standard error to /dev/null . /dev/null is a special device that discards everything that is written to it.

What does ${} mean in shell script?

Here are all the ways in which variables are substituted in Shell: ${variable} This command substitutes the value of the variable. ${variable:-word} If a variable is null or if it is not set, word is substituted for variable.


1 Answers

it's the Equal Tilde operator that allows the use of regex in an if statement.

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. Any part of the pattern may be quoted to force it to be matched as a string.

http://linux.die.net/man/1/bash

like image 159
MimiEAM Avatar answered Sep 21 '22 04:09

MimiEAM