PDA

View Full Version : BASH: if [ variable 'contains'...



ryanVickers
September 28th, 2007, 11:04 PM
ok, if the title wasn't descriptive enough, I'll elaborate...

I am in need of a kind of if statement that will allow me to check for if the variable contains a certain string rather than if it's equal to it.

so, what would I put in place of the "==" in the following code:



if [ $variable == "string" ]
then...

:confused:

mssever
September 28th, 2007, 11:29 PM
Untested code:

if [[ $var =~ regexp ]]; then
#do something
fi

ryanVickers
September 28th, 2007, 11:40 PM
ok, I tell you how it works :)

ryanVickers
September 28th, 2007, 11:42 PM
Thanks a lot! You've saves a lot of time and diskspace (well, no too much diskspace :))

Kakalle
October 7th, 2011, 10:48 AM
Hi,

what would be the opposite of tis statement?
I mean: IF var/string contains not

This is not working:
if [[ $var !~ regexp ]]; then
Thank you very much for your help !!!

nothingspecial
October 7th, 2011, 10:55 AM
Hi, this thread is very old.

Please create a new one with as much detail of your problem as possible.

Closed.