Meaning of '$#' in Unix

All,

In the below mentioned piece of code :

if test $# -eq 1
        then
            echo "Input parameter passed into DMI_weekly.ksh..." | tee -a $RUNLOG
            typeset -u ORACLE_SID
            export ORACLE_SID="$1"
        else
            echo "ERROR 060: Arguments passed to DMI_weekly.ksh do not equal 1..." | tee -a $RUNLOG
            echo "***Verify that a valid ORACLE_SID is being passed when calling script..." >> $RUNLOG
            echo "See $RUNLOG for more details..."
            echo "\nExiting DMI_weekly.ksh with exit status=1...\n"| tee -a $RUNLOG
            echo "Preload Error:  Arguments passed to DMI_weekly.ksh do not equal 1" |mailx -s "DMI_weekly.ksh (ERROR 070)" "
$MAILID"
        exit 1
    fi

what '$#' tells to the unix ...?

Regards
Oracle user

It is the special built-in variable which contains the total number of parameters passed to any shell script.

if you invoke script test.ksh in this way

./test.ksh arg1 arg2 arg3

then the value of $# inside the script would be 3.

This means the number of parameters (values) given to the script.
This small example will help you to understand :

Create the scritpt test.sh containing the following lines
echo "1st parameter : $1"
echo "2nd parameter : $2"
echo "3rd parameter : $3"
echo "Number of parameters: $#"
If you execute ./test.sh A B C

The output will be :
1st parameter : A
2nd parameter : B
3rd parameter : C
Number of parameters: 3
If now you execute ./test.sh A B

The output will be :
1st parameter : A
2nd parameter : B
3rd parameter :
Number of parameters: 2

Coming back to your script :

if test $# -eq 1 checks that one parameter is given to the script during its execution. Please notice this doesn't check the content of this parameter.

1 Like

Thank-you anchal and fhernu.
Even i was considering it in the same way, putted this in the forum, just to confirm it.

Regards
Oracle User