Pass RegEx to java program in bash script

I can't seem to get this right. I've tried it every way imaginable using every trick I see on stackexchange and such. No luck. So nothing major here, something like:

#!/bin/bash

SEARCH="ARG1 ARG2 '((^EXACT$)|(.*InTheMiddle*)|(^AtBeginning*))'"
java -cp /my/class/path MyClassName $SEARCH
exit $?

For the java program to work, the single quotes must be in there. I've tried escaping them all kinds of ways but can't get the script to call the java class in a way that is simply what it is. From the command line I can call the java class with no issue. Any help? Definitely not worth the time I've been spending on this... Thanks - Eric

The code that I would EXPECT to work is:

#!/bin/bash

SEARCH='ARG1 ARG2 "((^EXACT$)|(.*InTheMiddle*)|(^AtBeginning*))"'
java -cp /my/class/path MyClassName $SEARCH
exit $?

But bash -x shows that performing:

So it puts my double quotes in single quotes. I've tried all sorts of other stuff to no avail. Anybody have an answer or even a work around?

Looks good for me. Did you try "$SEARCH" ?

1 Like

Ahhhh, you got it! Had to separate the first 2 arguments that didn't need quoted from the last argument and surround the last one in quotes when sending it. Thank you!!!

#!/bin/bash

SEARCH1='ARG1 ARG2'
SEARCH2='((^EXACT$)|(.*InTheMiddle*)|(^AtBeginning*))'
java -cp /my/class/path MyClassName $SEARCH1 "$SEARCH2"
exit $?