Reputation: 17
function mem(){
local token
return $token
}
obj=$(ls) #list of object name
objstr=($obj) #array[i] whose ith element is object name
objno=$(echo "$obj" | wc -w) #the number of object name
for i in $(seq 0 $(($objno-1)))
do
objID=${objstr[$i]} #object name
IDlen=${#objID} #length of object name
for j in $(seq 0 $(($IDlen-1)))
do
subID=${objID:$j:1} #charater in the name
token=$(mem) #allocate local variable returned
token[0]+=$subID #concatenate characters
done
I want to define a variable in which I can store an objects name. (in a way that I can access each characters in the name) But the most important thing is that it has to be disposable. I want to 'free' the memory allocated to the variable before going on with another object name to minimize overhead. So I defined a function, mem() which would return local variable. If I initialize token[0] to the returned variable each time before I use it, what happens to the memory previously allocated to the variable? Is it 'free'ed as intended?
Upvotes: 0
Views: 2518
Reputation: 72707
You don't free memory in the shell; there are no explicit ways to free such things as the storage for variables. That said, you could try
unset NAME
once you're done with NAME
Note that just like in C, this doesn't guarantee the memory is returned to the OS.
But then, why do you worry about memory for shell variables in the first place? What is your actual problem? If memory use and management is a constraint, maybe the shell is the wrong tool?
Upvotes: 2