全部大写,用下划线分隔,声明在文件的顶部。
常量和任何导出到环境中的都应该大写。
# Constant
readonly PATH_TO_FILES='/some/path'
# Both constant and environment
declare -xr ORACLE_SID='PROD'
第一次设置时有一些就变成了常量(例如,通过 getopts)。因此,可以在 getopts 中或基于条件来设定常量,但之后应该立即设置其为只读。值得注意的是,在函数中 declare 不会对全局变量进行操作。所以推荐使用 readonly 和 export 来代替。
VERBOSE='false'
while getopts 'v' flag; do
case "${flag}" in
v) VERBOSE='true' ;;
esac
done
readonly VERBOSE