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?
至于bash,将文本输出存储在变量中是不是一种坏习惯?我的意思不是几行,而是几MB的数据。脚本完成后是否应该清空变量?
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?
编辑:我没有详细说明第二部分,我想问一下,如果我在当前shell中运行脚本,而不是在子shell中运行脚本,是否应该清空变量,这样它就不会耗尽内存。或者,我不应该在当前的脚本中运行脚本吗?
2 个解决方案
#1
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.
您需要了解脚本是在子shell(当前shell的子代)中执行的,它获取自己的环境和变量空间。当脚本结束时,该子shell存在并且该子shell保存的所有变量无论如何都被销毁/释放,因此不需要以编程方式清空变量。
#2
0
As for bash, is it a bad practice to store text output in variables?
至于bash,将文本输出存储在变量中是不是一种坏习惯?
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)
这是很好的做法!继续使用bash编程,并不关心这种内存问题(直到你想将debian DVD映像存储在单个$ debian_iso变量中,那么你可能会遇到问题)
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?
我的意思不是几行,而是几MB的数据。脚本完成后是否应该清空变量?
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
完成脚本执行后,bash shell中的所有变量都会蒸发。它会为你管理内存。也就是说,如果你指定foo =“bar”,你可以在同一个脚本中访问$ foo,但显然你不会在另一个脚本中看到$ foo
#1
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.
您需要了解脚本是在子shell(当前shell的子代)中执行的,它获取自己的环境和变量空间。当脚本结束时,该子shell存在并且该子shell保存的所有变量无论如何都被销毁/释放,因此不需要以编程方式清空变量。
#2
0
As for bash, is it a bad practice to store text output in variables?
至于bash,将文本输出存储在变量中是不是一种坏习惯?
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)
这是很好的做法!继续使用bash编程,并不关心这种内存问题(直到你想将debian DVD映像存储在单个$ debian_iso变量中,那么你可能会遇到问题)
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?
我的意思不是几行,而是几MB的数据。脚本完成后是否应该清空变量?
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
完成脚本执行后,bash shell中的所有变量都会蒸发。它会为你管理内存。也就是说,如果你指定foo =“bar”,你可以在同一个脚本中访问$ foo,但显然你不会在另一个脚本中看到$ foo