Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically building a command in bash

Tags:

I am construcing a command in bash dynamically. This works fine:

COMMAND="java myclass"
${COMMAND}

Now I want to dynamically construct a command that redirectes the output:

LOG=">> myfile.log 2>&1"
COMMAND="java myclass $LOG"
${COMMAND}

The command still invokes the java process, but the output is not redirected to myfile.log

Additionally, if I do:

BACKGROUND="&"
COMMAND="java myclass $BACKGROUND"
${COMMAND}

The command isn't run in the background.

Any clues on how to get the log redirect, and background bits working? (bash -x shows the commands being constructed as expected)

(In reality, unlike this example, the values of LOG and BACKGROUND are set dynamically)

like image 224
Joel Avatar asked Nov 11 '09 14:11

Joel


1 Answers

You could do it with the eval command:

eval ${COMMAND}
like image 188
tangens Avatar answered Sep 27 '22 20:09

tangens