Q:

Groovy how to combine multiple commands?

I want to exec this command to get vertxHome.
In shell, I can do it like this:
vertxHome=$(dirname $(dirname $(readlink -f $(which vertx))))
echo $vertxHome

But how to do this in groovy?
I tried vertxHome = 'dirname $(dirname $(readlink -f $(which vertx)))'.execute(), but it not works.

A:

Try executing the command through the shell instead of execute the command directly, try with:
["/bin/sh", "-c", "your_command"].execute()
in your case: 
["/bin/sh", "-c", "dirname \$(dirname \$(readlink -f \$(which vertx)))"].execute()
Note that as OP advice in his comment $ must be escaped in the command.
If you want to get the output for the command you can get it through this sample code which it is from in groovy documentation:
//A string can be executed in the standard java way:
def proc = ["/bin/sh", "-c", "dirname \$(dirname \$(readlink -f \$(which vertx)))"].execute()             
// Call *execute* on the string
proc.waitFor()                               // Wait for the command to finish

// Obtain status and output
println "return code: ${ proc.exitValue()}"
println "stderr: ${proc.err.text}"
println "stdout: ${proc.in.text}" // *out* from the external program is *in* for groovy

If you want to access to an environment variable and don't care about command execution use: java.lang.System.getenv(), in groovy for example this could be:
def javaHome = java.lang.System.getenv("JAVA_HOME")
print javaHome

You can do the same to get VERTX_HOME.
Hope this helps,

