2

I use the shell script to reset variable.

 #!/bin/sh 
reset_var() { 
while [ "$#" -gt "0" ] do  
 echo "\$1 is $1"   
unset $1 done 
} 

i=50  
j= 40 
reset_var i j

but this it does not work!

the purpose is to reset i and j variable to 0

is there any way to reset many variables !

Anis_Stack
  • 3,306
  • 4
  • 30
  • 53

2 Answers2

5

In your situation, you do not need a reset_var function, simply do:

i=50
j=40
unset i j

That is said, a possible reset_var function would be:

reset_var() {
  while [ "${#}" -ge 1 ] ; do 
   unset "${1}"
   shift
  done
}
Jay jargot
  • 2,745
  • 1
  • 11
  • 14
0

You have an infinite loop when passing some arguments. Your reset function should look something like this:

reset_var() {
for arg
do
  unset ${arg}
done
}
Kubator
  • 1,373
  • 4
  • 13