Monday, June 15, 2009

Bash: Evaluating a Command from a Variable with a Redirect

In bash, I was assigning a command I wanted to run to a variable while redirecting the output:
  command="mvn clean verify > /tmp/out"
$command
The redirect greater-than sign was being passed to the mvn command. Placing an eval in front of the mvn command does the trick:
  command="eval mvn clean verify > /tmp/out"

2 comments:

  1. You can also put quotes of course around the command you're eval'ing:

    $ eval 'ls' > /tmp/out

    ReplyDelete
    Replies
    1. This helped me six years after you posted. Thanks John!

      Delete