21

(I'm editing an existing Bash script, so I'm probably making a silly mistake here...)

I have a shell script that saves a command with an environment variable as its argument like this:

COMMAND="mvn clean install -P $MAVEN_PROFILE"

It then executes the command with nohup roughly as follows:

nohup $COMMAND > logfile

This works.

Now, I want to set an environment variable that can be accessed in Maven. I've tried several things like the following:

COMMAND="FORMAVEN=valueForMaven mvn clean install -P $MAVEN_PROFILE"

...but then it just terminates with:

nohup: failed to run command `FORMAVEN=valueForMaven': No such file or directory

I feel like there are several unrelated concepts at work here, none of which I understand or even know about. What do I need to be able to do the above?

1 Answer 1

32

Three methods:

  • set (and export) the variable before launching mvn

  • set the variable on the nohup launch:

    FORMAVEN=valueForMaven nohup $COMMAND > logfile
    
  • use env to set the variable

    COMMAND="env FORMAVEN=valueForMaven mvn clean install -P $MAVEN_PROFILE"
    
2
  • Excellent! I knew it had to be easier than the directions I was thinking of...
    – Vincent
    Sep 7, 2016 at 8:52
  • How come I never realised that it was as simple as export THEN run the command??? Thanks! May 20, 2020 at 0:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .