URGENT: bash positional parameters

Hi!

I'm writing a script for launching java-application. It should pass all the parameters to java:

$JAVA_HOME/bin/java -cp . MyAppplication $*

This works fine if all the arguments consist of one word. But there's a problem for multiple words:

./myscript 'hello world'

$* gives hello world striping off the quotes. It means that java gets two arguments instead of one. If I launch java directly it works fine:
$JAVA_HOME/bin/java -cp . MyApplication 'hello world'
This gets one argument, exactly what I need.

How can this be solved? How can I leave quotes when passing arguments to java?

Thanks in advance!
LVL 4
vk33Asked:
Who is Participating?
 
mbekkerCommented:
The following solution even works for multiple arguments with multipe words:

$JAVA_HOME/bin/java -cp . MyAppplication "$@"
0
 
shivsaCommented:
u can create new variable like
new="\"$*\"";
$JAVA_HOME/bin/java -cp . MyAppplication $new

it should work.
0
 
vk33Author Commented:
Thanks a lot!

Could you please explain me how this quoting works? I don't really understand the difference between $@ (as I tried) and "$@".

Thanks!
0
 
mbekkerCommented:
You're welcome!

Using $@, the parameters will be expanded to positional parameters. So far, no problem. But when these positional parameters are placed after each other, they loose there "positionalness"...

e.g. parameters "Hello World" Var2 "Var three" will be expanded to:
Hello World
Var2
Var three

but when placed after each other: Hello World Var2 Var three

And this is the case your command line. Every word will be interpreted as one parameter.

When "$@" is used, every positional parameter is placed within double quotes:

"Hello World"
"Var2"
"Var three"

so it makes: "Hello World" "Var2" "Var three"
0
 
vk33Author Commented:
OK, thanks a lot! :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.