Solved

URGENT: bash positional parameters

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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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.

770 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