Solved

URGENT: bash positional parameters

Posted on 2003-11-26
5
320 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
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 navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

920 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

17 Experts available now in Live!

Get 1:1 Help Now