Reputation: 1078
If you pass an arg to a java app via a bash script, and the arg contains whitespaces, how can you make the java app see the entire string as the arg and not just the first word? For example passing the following two args via bash to java:
var1="alpha beta zeta"
var2="omega si epsilon"
script.sh $var1 $var2
(inside script.sh)
#!/bin/bash
java -cp javaApp "$@"
(inside javaApp)
param1 = args[0];
param2 = args[1];
The values of my param variables in javaApp are getting only the words, not the entire lines:
"param1 is alpha"
"param2 is beta"
What can I change in the javaApp to see the entire string being passed in via args[] as the argument and not just the first word it encounters?
Upvotes: 3
Views: 5859
Reputation: 31274
the problem is that you are merging the variables already when calling script.sh
(and once they are merged, there is no way to split them again).
so do something like:
var1="alpha beta zeta"
var2="omega si epsilon"
script.sh "$var1" "$var2"
Upvotes: 0
Reputation: 410562
You should wrap the arguments in quotes when you call script.sh
:
$ script.sh "$var1" "$var2"
These lines:
var1="alpha beta zeta"
var2="omega si epsilon"
don't actually include the quotes in var1
's and var2
's strings.
Upvotes: 1
Reputation: 121702
The problem is the way your shell script is written, not your Java program.
You need to quote the arguments:
script.sh "$var1" "$var2"
Upvotes: 7