在shell脚本中使用只读变量 [英] Use of read-only variables in shell scripts
问题描述
这是好壳的编程习惯使用只读变量只要有可能还是有什么缺点?例如。如果我想要写一些脚本,由多个脚本文件使用的不可变的文件路径,这将是有意义的声明这样的路径:
Is it good shell programming practice to use read-only variables whenever possible or does it have any drawbacks? E.g. if I wanted to write some script that consists of multiple script files that make use of immutable file paths, would it make sense to declare the paths like that:
readonly LOGS
export LOGS
LOGS="/some/path"
另一个问题:这是个好主意分裂单片和过于繁琐的阅读shell脚本code到单独的文件?
非常感谢您的回答。
Another question: Is it a good idea to split monolithic and tedious too read shell script code into separate files? Many thanks for your answers.
推荐答案
这听起来像你可能会认为只读
不超过它确实。一方面,只读状态不会导出到环境或由子进程继承:
It sounds like you might think that readonly
does more than it really does. For one thing, readonly status is not exported into the environment or inherited by child processes:
$ declare -rx LOGS=hello
$ LOGS=goodbye
bash: LOGS: readonly variable
$ bash -c 'echo "$LOGS"'
hello
$ bash -c 'LOGS=goodbye; echo "$LOGS"'
goodbye
$
这篇关于在shell脚本中使用只读变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!