Dan
Dan

Reputation: 378

How to search for specific characters within a string?

String :

 [email protected]

Want to check for :

@
.com

if those two are found, I want to set my boolean true

bool_one=true

How do I do this? I tried

if [ $word|grep[@] = $word]

but it's not working.

Upvotes: 0

Views: 32

Answers (2)

clt60
clt60

Reputation: 63902

you can also

s='[email protected]'

[[ "$s" =~ (.*)@(.*)\.com ]] && echo "name: ${BASH_REMATCH[1]} domain ${BASH_REMATCH[2]}.com"

Upvotes: 0

anubhava
anubhava

Reputation: 785068

You can do it like this:

s='[email protected]'

[[ "$s" == *@*.com ]] && echo "true"

Upvotes: 2

Related Questions