by zkhalique · 9y ago · 0 comments
If it's an issue for you on Linux, you can always run a python shell and spawn your process there. On Windows you can't.
If you want spaces to be unparsed in parameters, you "quote" the parameter.
A="spaces in the argument"
touch "$A"
ls -l
total 0
-rw-rw-r-- 1 me me 0 Dec 27 15:40 a file with spaces PROPS="-Dprop1=foo bar -Dprop2=bar foo"
java $PROPS SomeClass.class
You can try to escape with backslashes and what not but it becomes fairly hard if not impossible to expand multiple parameters correctly as a single variable. I believe one solution is to use arrays and another is just not use arguments and instead rely on other configuration mechanisms (env variables, files, etc).You see this often rear its ugly head with daemon scripts.
I left an example of an argument with a space in there (the last one).
PROPS=("-Dprop1=foo" "bar" "-Dprop2=bar foo")
java "${PROPS[@]}" SomeClass.class
(The quotes around `${PROPS[@]}` is important.) Do note, there is an unfortunate edge case here if PROPS is empty, you'll get a false `""` arg passed to java in that case. There's a less pleasant syntax that avoids that issue but I don't recall it off-hand.(edit: Please read the replies to my post, I didn't think about the fact that this syntax is bash specific. Thanks to those who pointed it out)
PROPS="'-Dprop1=foo bar' '-Dprop2=bar foo'"