为了忘记它,我在我.bashrc
的每个 Bash shell 脚本文件的顶部附近定义了以下内容:
# Allow to define an alias.
#
shopt -s expand_aliases
# Defines a function given a name, empty parentheses and a block of commands enclosed in braces.
#
# @param name the name of the function.
# @param parentheses the empty parentheses. (optional)
# @param commands the block of commands enclosed in braces.
# @return 0 on success, n != 0 on failure.
#
alias def=function
# Defines a value, i.e. read-only variable, given options, a name and an assignment of the form =value.
#
# Viable options:
# * -i - defines an integer value.
# * -a - defines an array value with integers as keys.
# * -A - defines an array value with strings as keys.
#
# @param options the options. (optional)
# @param name the name of the value.
# @param assignment the equals sign followed by the value.
# @return 0 on success, n != 0 on failure.
#
alias val="declare -r"
# Defines a variable given options, a name and an assignment of the form =value.
#
# Viable options:
# * -i - defines an integer variable.
# * -a - defines an array variable with integers as keys.
# * -A - defines an array variable with strings as keys.
#
# @param options the options. (optional)
# @param name the name of the variable.
# @param assignment the equals sign followed by the value. (optional)
# @return 0 on success, n != 0 on failure.
#
alias var=declare
# Declares a function as final, i.e. read-only, given a name.
#
# @param name the name of the function.
# @return 0 on success, n != 0 on failure.
#
alias final="readonly -f"
上面的定义让我可以说例如:
def foo { echo bar; }
.
final foo
var foo=bar
val foo=bar
如注释所示,您可以混合和匹配各种变量标志,例如var -g foo=bar
全局(-g)变量(var)或val -Ai foobar=([foo]=0 [bar]=1)
只读(val),由整数(-i)值组成的关联数组(-A) .
这种方法也带有隐式变量范围。此外,任何使用 JavaScript、Java、Scala 等语言编程的软件工程师都应该熟悉新引入的关键字def
、和。val
var
final