Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a Bash script which installs a customised JBoss. The name of the JBoss instance is checked against a regular expression to make sure that the name is valid:

if [[ ! $1 =~ $instanceNameRegex ]]; then
    exit 1

We now have a requirement for a second valid regex. ie the name must match either $instanceNameRegex or $secondInstanceNameRegex.

How do I achieve this in the if test? I'm looking for this, but I can't get it to work:

if [[ [ ! $1 =~ $instanceNameRegex ] -a [ ! $1 =~ $secondInstanceNameRegex ] ]]; then

ie if the parameter doesn't match the first regex AND it doesn't match the second, then exit.

share|improve this question
Since when has [ been an argument to [[? – Ignacio Vazquez-Abrams Feb 2 '12 at 9:59
That's why I'm asking on here: I'm a Java programmer! – Rich Feb 2 '12 at 10:04
up vote 4 down vote accepted

You can do

if [[ ! $1 =~ $instanceNameRegex && ! $1 =~ $secondInstanceNameRegex ]]; then

I would use instead:

if [[ ! ( $1 =~ $instanceNameRegex || $1 =~ $secondInstanceNameRegex ) ]]; then
share|improve this answer

This might work for you:

if [[ ! $1 =~ "${instanceNameRegex}|${secondInstanceNameRegex}" ]]; then
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.