且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Shell脚本中创建别名?

更新时间:2023-12-05 18:54:40

来自 bash 手册页:> p>

From the bash man page:


当shell非交互式时,别名不会扩展,除非 expand_aliases shell选项是使用 shopt 进行设置(请参阅 SHELL BUILTIN COMMANDS 下的 shopt
说明

Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt (see the description of shopt under SHELL BUILTIN COMMANDS below).

所以这应该起作用:

#!/bin/bash
shopt -s expand_aliases
alias I_am_only_ls_alias=ls
I_am_only_ls_alias

脚本通常使用函数,而不使用别名。

Scripts usually use functions, not aliases.