Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: how to search for a substring in a parameter?



On Jan 13,  4:13pm, Carlos Carvalho wrote:
}
} I'm trying to see if the value of a parameter contains a given string

The right-hand-side of == in a [[ ]] construct is a glob pattern, so if
all you want is true/false, this works:

    if [[ $foo == *pattern* ]]; then
 	# contains pattern
    else
 	# doesn't contain pattern
    fi

If you want to extract the matching string, see the M and S flags:

    matching=${(SM)foo##pattern}



Messages sorted by: Reverse Date, Date, Thread, Author