Resetting the Positional parameters values

Hi,

Can any one provide the Unix command to reset the positional parameters?

Please see the below example where i have to pass 2 parameters to Shell1.sh.

Step1) . ./Shell1.sh 2 3
successfully executed, Then i executed(next step only) the same shell script again,this time no parameters passed.

Step 2) . ./Shell1.sh
This is also successfully executed,because it considered(previous values) 2 & 3 as the positional parameters as i had not reseted them.

Thanks in advance.

I don't understand. How does the script know of the former parameters? Do you export them or write them to file?

Hi zaxxon ,

How does the script know of the former parameters?
As i know if we once assign the values to positional parameters those will be remain same until we reassign them.the same happened in my case also.

Do you export them or write them to file?
I am not exporting or not writing into a file.

I am new to Unix & shell scripting, correct me if i am wrong correct me in above.

Below is the content of Shell1.sh:
if [ $# != 2 ]
then
echo "No Of Parameters passed not equal to 2"
else
echo $1
echo $2
echo "Completed"
fi

Below is the flow of execution & their output:

. ./Shell1.sh 2 3
2
3
Completed
. ./Shell1.sh
2
3
Completed

The second time i need to get the output like "No Of Parameters passed not equal to 2", but it was not happened.This is the reason i need to know the command to reset the positional parameters, can u share the command if you know.

Thanks for your help.

This question has been raised a number of times on this forum. What you are seeing is the expected behavour of your shell since you are dot including i.e. sourcing Shell1.sh.

Now I understand what you mean ok...
There is nothing to reset as shell scripts usually don't remember anything without writing it to a file or exporting it to the environment. So there is nothing to reset.
The problem with your script is the line

which you should try with

if (( $# != 2 ))

The double round brackets treat the values as numbers (arithmetic), not as strings, so it should work now.

Example:

root@isau02:/data/tmp/testfeld> cat script.ksh
#!/usr/bin/ksh

echo $#

if (( $# != 2 )); then
        echo "Hey! This is not 2 parameters!"
else
        echo "Everything is fine"
fi

exit 0
root@isau02:/data/tmp/testfeld> ./script.ksh  b lala
2
Everything is fine
root@isau02:/data/tmp/testfeld> ./script.ksh  b lala lal
3
Hey! This is not 2 parameters!
root@isau02:/data/tmp/testfeld> ./script.ksh  yo
1
Hey! This is not 2 parameters!

EDIT: Oh didn't notice he was sourcing, forget my post :wink: