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?
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 !=
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.
(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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With