1830207 Members
1996 Online
109999 Solutions
New Discussion

Few Questions

 
SOLVED
Go to solution
Hunki
Super Advisor

Few Questions

1. whats $# in a shell script
2. whats a $* in shell script
3. If $1 is the first parameter, whats $0

I tried echoing $# and it gave me 0 , i tried $? and I got 0 , I tried $* and it gave me nothing ... what does it mean.

Thanks,
5 REPLIES 5
Keith Johnson
Valued Contributor
Solution

Re: Few Questions

1. $# is set to the number of arguments passed to the script through the command line.

2. $* is set to the actual arguments passed to the script through the command line.

3. $0 is set to the name of the script you executed.
No matter where you go...there you are.
spex
Honored Contributor

Re: Few Questions

Hunki,

These variables, and other POSIX shell concepts, are described in sh-posix(1):

+ The following parameters are set automatically by the shell:

0 The string used to call the command or script, set from
invocation argument zero.

1, 2, ... The positional parameters.

*, @ All the set positional parameters, separated by a field
separator character. See the "Quoting" subsection.

# The number of set positional parameters in decimal.

- Flags supplied to the shell on invocation or by the set
command.

? The decimal exit status returned by the last executed
command.

$ The process number of this shell.

_ Initially, the absolute path name of the shell or
script being executed, as passed in the environment.
Subsequently, it is assigned the last argument of the
previous command. This parameter is not set for
commands which are asynchronous. This parameter is
also used to hold the name of the matching MAIL file
when checking for mail.

! The process number of the last background command
invoked.

PCS
Patrick Wallek
Honored Contributor

Re: Few Questions

If you are using the POSIX shell (/sbin/sh or /usr/bin/sh) then have a look at the "Parameter Substitution" section of the sh-posix man page.

The sh-posix man page starts on page 340 in this document:
http://docs.hp.com/en/B2355-90903/B2355-90903.pdf

The "Parameter Substition" section is on page 351.
Hunki
Super Advisor

Re: Few Questions

Thanks , 10 points each.
Matti_Kurkela
Honored Contributor

Re: Few Questions

$# = number of parameters given to the script.

$? = result code of the previous command. The specific meaning of each command's result codes can be found from the command's man page, but the general definition is that 0 means "OK" and anything else "some error occurred".

$* = all parameters given to the script, in a single string.
Usually, "$@" (with the quotes) is more useful than this.

$0 = the command used to start the script.
This might be useful if the script needs to re-start itself, for example.

Example "test1.sh":

#!/bin/sh
echo "$0: $# args"
echo "\"$*\""
for i in "$@"
do
echo -n "\"$i\" "
done
echo

Try all these ways to start it:
sh test1.sh
sh test1.sh A
sh test1.sh A B
sh test1.sh A B C
sh test1.sh "arg with spaces" "another arg"
sh $PWD/test1.sh A B C

Read "man sh-posix" to find more information. It's a *long* man page.

MK
MK