Anis_Stack
Anis_Stack

Reputation: 3452

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 !

Upvotes: 2

Views: 10430

Answers (2)

Jay jargot
Jay jargot

Reputation: 2868

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
}

Upvotes: 5

Kubator
Kubator

Reputation: 1383

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
}

Upvotes: 0

Related Questions