Wednesday, April 7, 2010

$@ is not "$@"

when you want to pass multiple parameters through you shell script, you use $@

#! /bin/sh
echo $@

and then execute this file:
./script1 hello world

the script will prints "Hello world"

But be ware, $@ is not "$@" !!

The story is, I wrote some Java file that accepts 1 argument from the command line, such like this:

public class SayHello{
public static void main(String[] args){
if (args.length != 1){
System.out.println("Usage: SayHello ");
// do other staff here

I have wrapped this program in a shell file "":
#! /bin/sh
java SayHello $@

But the result was unexpected, as when I run with this command:
./ "Mohammed Hewedy"

I got
Usage: SayHello

I found the problem in the shell file, that I should use "$@" instead of $@
So, the script should be:
#! /bin/sh
java SayHello "$@"

No comments: