based on Eric Renouf's comments on previous iteration of this answer on how nohup
and bash
conspire to thwart escaping spaces
user="Tim Toms"
jarfile=./app.jar
SC_CD="java -jar -Xms512m -Xmx2048m -DUSER='$user' $jarfile"
echo $SC_CD > temp.sh
nohup bash temp.sh
If the point was to let that java run in the background, I might have just done something like
echo java -jar -Xms512m -Xmx2048m -DUSER=\'$user\' $jarfile | at now
and forgotten about nohup
(so what if maybe there's some trash email to clean up)
==== old answer below, just so comments make sense. Forget below otherwise ====
While it seems your statements actually do call nohup
with "Tim Tom" in single quotes, perhaps nohup
does its system()
call with a simple string instead of making an exec
call, and making the string loses the quotes.
I just tested by putting an echo
in front of the nohup
I am not really in a position to test it, but I suggest try making your first line into
user='"Tim Tom"'
as single quotes are supposed to prevent expansion and should pass the double quotes on to nohup
I'm basing this on making a test script like
ser="Tim Tom";
jarfile=./app.jar
SC_CD="java -jar -Xms512m -Xmx2048m -DUSER='$user' $jarfile"
echo nohup $SC_CD
then when I sh -x test.sh
I get
+ user='Tim Tom'
+ jarfile=./app.jar
+ SC_CD='java -jar -Xms512m -Xmx2048m -DUSER='\''Tim Tom'\'' ./app.jar'
+ echo nohup java -jar -Xms512m -Xmx2048m '-DUSER='\''Tim' 'Tom'\''' ./app.jar
nohup java -jar -Xms512m -Xmx2048m -DUSER='Tim Tom' ./app.jar
But making the suggested change gives
+ user='"Tim Tom"'
+ jarfile=./app.jar
+ SC_CD='java -jar -Xms512m -Xmx2048m -DUSER='\''"Tim Tom"'\'' ./app.jar'
+ echo nohup java -jar -Xms512m -Xmx2048m '-DUSER='\''"Tim' 'Tom"'\''' ./app.jar
nohup java -jar -Xms512m -Xmx2048m -DUSER='"Tim Tom"' ./app.jar
Note that in both cases, echo shows that nohup should be getting at least the first level of quotes in its arguments. That's why I am suggesting adding an additional level
nohup java -jar -Xms512m -Xmx2048m -DUSER="$user" "$jarfile"
instead of having those on different lines? – Eric Renouf May 05 '16 at 12:30bash
instead of/bin/sh
, you could usebash
arrays. – Wildcard May 05 '16 at 19:45