1833424 Members
2855 Online
110052 Solutions
New Discussion

Script question

 
SOLVED
Go to solution
Nobody's Hero
Valued Contributor

Script question

What does '$#' represent ?

I know '$?' is return code. Not sure about '$#'
UNIX IS GOOD
4 REPLIES 4
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Script question

It's the number of arguments. Inside a function, it's the number of arguments passed to the function otherwise it's the number of argument passed on the command line. Note: The "shift" command, reduces ${#} so after doing a shift 2, for example, ${#} is now 2 less than the number actually passed.
If it ain't broke, I can fix that.
Mel Burslan
Honored Contributor

Re: Script question

number of arguments after the command on the command line is represented by $#

command parm1 parm2 parm3

if in the command, you have a line like echo $#, you will see 3 as output to the command line above

Hope this helps
________________________________
UNIX because I majored in cryptology...
Sandman!
Honored Contributor

Re: Script question

It's the number of arguments passed to a script excluding any command line options (which need to be parsed with the getopts command). Within the script the command line arguments equate to the positional parameters $1, $2, $3 all the way upto $9; with $0 being the name of the script itself.

So if your script is named "script.ksh" and it takes arguments and options like...

# script.ksh argument1 argument2 -o argument3
| | | |
$0 $1 $2 $3

hope this helps!
Basheer_2
Trusted Contributor

Re: Script question

$# or ${#} is the count of args

eg:
cmd arg1 arg2 arg3

${#} = 3
${1} = arg1
${2} = arg2
${3} = arg3
${*} = "arg1 arg2 arg3"
${@} = "arg1" "arg2" "arg3"