Archive for September, 2010

Using Linux command line arguments containing spaces in a variable

Last days had some trouble with space characters used in file names and the consequences of passing parameters using those file names to Linux programs.
Consider the following example:
cmdline="-DMyTest=\"1 2 3\" -some \"./Arguments.test\" -testpath=\"/my test\" \"my test\""
echo ----------------------------------------------
./ $cmdline
echo ----------------------------------------------
./ -DMyTest="1 2 3" -some "./Arguments.test" -testpath="/my test" "my test"
echo Using '"$@"';
for p in "$@";
echo "[$p]";

Calling, the result is:
Using "$@"
Using "$@"
[-DMyTest=1 2 3]
[-testpath=/my test]
[my test]

– where the first part is an issue as it does not read my arguments as expected.
The solution was very simple, in the first call use
eval ./ $cmdline
instead. Although the solution is really simple, it was difficult to Google anything useful thus this post.
Finally found a hint in

P.S. If you need to call a Linux shell command using Java – Runtime.exec() method, use an array as the input and DON’T try to wrap every argument in spaces. It works on Windows but not Linux.

Pinky and the Brain was a true story after all

Two music videos with some relation to NL :)


South-park character