Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I just started doing shell scripts and getting unknown operand error while using
regex
in if statement. i searched google but did not get anything
IP="172.21.1.1"
if [[ "$IP" =~ /d ]] ; then
echo "qqq"
Getting error as
sh: =~: unknown operand
Bash version is : BusyBox v1.19.3 (2012-01-31 08:57:52 PST) built-in shell (ash)
–
–
–
–
This is happening because the operator =~
doesn't existe for bash.
As see you are trying to use a Regex to compare your variables. I recommend to use the expr
command. Here is an example:
IP="172.21.1.1"
if [[ $(expr match "$IP" 'my_regex') != 0 ]]; then echo "qqq"; fi;
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.