Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

URGENT: bash positional parameters

Posted on 2003-11-26
5
Medium Priority
?
370 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 800 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
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.

704 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