首页 > 解决方案 > 别名的有效形式是什么?

问题描述

我正在尝试在 bash 中实现 alias 命令的基本行为,我只想知道名称的有效形式。

标签: bashunixalias

解决方案


我看了一下 Bash 源代码,@chepner 的评论是正确的。任何未被 shell 解释的字符已经是有效的。所以像$,/和之类的东西=是不允许的。

来自general.c,https: //ftp.gnu.org/gnu/bash/bash-5.0.tar.gz

/* Return 1 if STRING comprises a valid alias name.  The shell accepts
   essentially all characters except those which must be quoted to the
   parser (which disqualifies them from alias expansion anyway) and `/'. */
int
legal_alias_name (string, flags)
     const char *string;
     int flags;
{
  register const char *s;

  for (s = string; *s; s++)
    if (shellbreak (*s) || shellxquote (*s) || shellexp (*s) || (*s == '/'))
      return 0;
  return 1;
}

推荐阅读