I have script which runs java binary and want to pass complex command which will be executed when OutOfMemoryException happens. Problem is more general and is not related to OOME. How can I put commands in variable and pass it to other script which will execute it later? This is the code I'm trying to execute. I tried different options of escaping, but without any result. During execution shell tries to split JAVA_OPTS on spaces, no matter how I escape it. #!/bin/sh
JAVA_OPTS="-Xmx10m"
JAVA_OPTS="$JAVA_OPTS \"-XX:OnOutOfMemoryError=echo 'Ups'\""
set -x
java $JAVA_OPTS TestMemory
I found this:BashFAQ 050, but none of covered cases applies on me. I can't put this code in a function as JVM process will have no access to it. When I put the whole command as one line, it works well but my use case is a little bit different and I can't modify the last line. JAVA_OPTS should be constructed somewhere else and then used as shown in the sample code.