别名(Alias)
虽然在 .bashrc 文件中很常见,但在脚本中应避免使用别名。
正如
Bash 手册
所述:
几乎在所有用途中,Shell 函数都优于别名。
别名使用起来很麻烦,因为它们需要仔细地引用和转义其内容,而且错误可能难以发现。
# this evaluates $RANDOM once when the alias is defined,
# so the echo'ed string will be the same on each invocation
alias random_name="echo some_prefix_${RANDOM}"函数提供了别名功能的超集,应始终优先使用。
random_name() {
echo "some_prefix_${RANDOM}"
}
# Note that unlike aliases function's arguments are accessed via $@
fancy_ls() {
ls -lh "$@"
}Last updated on