Search code examples
shellash

Reset variable using shell


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 !


Solution

  • 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
    }