一次创建多个 shell 别名

Jos*_*muk 8 command-line alias

我想在别名中插入不同可能的拼写变体,例如cat命令。我可以使用一些符号来表示“或”还是应该在新行上?

alias at|cart|cst '/bin/cat'
Run Code Online (Sandbox Code Playgroud)

mur*_*uru 17

帮助alias表明它可以一次分配多个别名:

alias: alias [-p] [name[=value] ... ]
    Define or display aliases.

    Without arguments, `alias' prints the list of aliases in the reusable
    form `alias NAME=VALUE' on standard output.

    Otherwise, an alias is defined for each NAME whose VALUE is given.
    A trailing space in VALUE causes the next word to be checked for
    alias substitution when the alias is expanded.
Run Code Online (Sandbox Code Playgroud)

所以你可以使用大括号扩展来生成name=value对:

alias {at,cart,cst}='/bin/cat'
Run Code Online (Sandbox Code Playgroud)

所以:

$ alias {at,cart,cst}='/bin/cat'
$ type at cart cst
at is aliased to `/bin/cat'
cart is aliased to `/bin/cat'
cst is aliased to `/bin/cat'
Run Code Online (Sandbox Code Playgroud)

也就是说,查看 zsh,它具有内置的错字更正(这对 没有帮助at,但对其他人有帮助):

% setopt correct
% sl
zsh: correct `sl' to `ls' [nyae]? y
% setopt correctall
% ls x.v11r4
zsh: correct `x.v11r4' to `X.V11R4' [nyae]? n
/usr/princton/src/x.v11r4 not found
% ls /etc/paswd
zsh: correct to `/etc/paswd' to `/etc/passwd' [nyae]? y
/etc/passwd
Run Code Online (Sandbox Code Playgroud)

如果y在 shell 询问您是否要更正某个单词时按,它将被更正。如果您按n,它将被单独留下。按a中止命令,并按下e带来阵容再次编辑,如果你同意这个词拼写错了,但你不喜欢的修正。


pLu*_*umo 7

我认为您不能一次分配多个别名。
但是您可以循环遍历这样的列表:

for a in cart xat vat xst cst vst dog; do alias "$a"='/bin/cat'; done
Run Code Online (Sandbox Code Playgroud)

确保别名尚未被其他程序使用(如 at您的示例)。

  • @JosefKlimuk:听起来值得自己回答。:-) (2认同)
  • @JosefKlimuk:不。我的意思是你应该根据你之前的评论为*这个问题*写一个正确的答案。 (2认同)