Solved

URGENT: bash positional parameters

Posted on 2003-11-26
5
314 Views
Last Modified: 2010-04-21
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!
0
Comment
Question by:vk33
  • 2
  • 2
5 Comments
 
LVL 24

Expert Comment

by:shivsa
ID: 9823173
u can create new variable like
new="\"$*\"";
$JAVA_HOME/bin/java -cp . MyAppplication $new

it should work.
0
 
LVL 1

Accepted Solution

by:
mbekker earned 200 total points
ID: 9823281
The following solution even works for multiple arguments with multipe words:

$JAVA_HOME/bin/java -cp . MyAppplication "$@"
0
 
LVL 4

Author Comment

by:vk33
ID: 9824201
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
 
LVL 1

Expert Comment

by:mbekker
ID: 9824932
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
 
LVL 4

Author Comment

by:vk33
ID: 9825088
OK, thanks a lot! :)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now