1

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?

1
  • 1
    As long as it's text, no problem at all. As soon as you need to store binary content, that's a problem -- NUL bytes can't be represented in C strings, which are how bash scalars are stored. Commented Mar 23, 2014 at 14:52

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

How are you actually running a script in current shell and why?
With source (builtin command). I get now that the variables are one of the reasons why I should execute them rather than run in current shell, is it right?
source command is used only when you need variables in current shell. If you are talking about clearing them then you don't need source command.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.