lubos
lubos

Reputation: 13

Storing text output in variables (bash)

As for bash, is it a bad practice to store text output in variables? I don't mean few lines, but even as much as few MB of data. Should the variables be emptied after the script is done?

Edit: I didn't clarify the second part enough, I wanted to ask whether I should empty the variables in the case I run scripts in the current shell, not in a subshell, so that it doesn't drain memory. Or, shouldn't I run scripts in the current one at all?

Upvotes: 1

Views: 535

Answers (2)

Jakub M.
Jakub M.

Reputation: 33827

As for bash, is it a bad practice to store text output in variables?

That's great practice! Carry on with bash programming, and don't care about this kind of memory issues (until you want to store debian DVD image in a single $debian_iso variable, then you can have a problem)

I don't mean few lines, but even as much as few MB of data. Should the variables be emptied after the script is done?

All you variables in bash shell evaporate when you finish executing your script. It will manage the memory for you. That said, if you assign foo="bar" you can access $foo in the same script, but obviously you won't see that $foo in another script

Upvotes: 0

anubhava
anubhava

Reputation: 784958

Should the variables be emptied after the script is done

You need to understand that a script is executed in a sub shell (child of the present shell) that gets its own environment and variable space. When script ends, that sub-shell exists and all the variables held by that sub-shell get destroyed/released anyway so no need to empty variables programmatically.

Upvotes: 1

Related Questions