johnny-b-goode
johnny-b-goode

Reputation: 3893

Unix $# statement

I have found '$#' statement at the very beginning of utility ksh script but i was unable to found any info about what does '$#' means. Usage:

if [[ $# -ne 1 ]];then
LogMessage "Usage:\nJavaWSJBossInstall! war_file"
exit
fi

Can anyone explain what does it mean? Thanks a lot.

Upvotes: 1

Views: 1074

Answers (5)

Mithrandir
Mithrandir

Reputation: 25337

The variable # contains the number of arguments passed to the script. if you call you script like this:

 script foo bar

then $# will give the you value 2.

Upvotes: 0

cdarke
cdarke

Reputation: 44354

See man ksh. $# gives the number of command-line parameters. The if statement could also be written as:

if (( $# != 1 ));

Upvotes: 0

Thorsten Kranz
Thorsten Kranz

Reputation: 12755

$# denotes the number of command line arguments supplied to the script. In your case it is checked whether exactly one argument was passed to it, otherwise some string explaining the usage is printed.

Upvotes: 1

kmkaplan
kmkaplan

Reputation: 18960

The shell variable # is the number of arguments to your script. You can access them as $1, $2 and so on.

Upvotes: 3

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272517

It evaluates to the number of command-line arguments passed to the script.

The best place to look to discover this kind of thing is the Bash manual. And in this particular case, you're interested in the section on special parameters. (oops, this is ksh, not Bash).

Upvotes: 1

Related Questions