一次性创建多个 shell 别名

我想在别名中插入不同的拼写变体,例如cat命令。我可以使用某个符号表示“或”吗,还是应该换行进行?
alias at|cart|cst '/bin/cat'

7让我想起了我有的一个别名:alias kk='ll' - pLumo
14我不建议将at作为cat的别名使用。实际上,有一个用于调度任务的命令at:https://askubuntu.com/a/339301/295286 - Sergiy Kolodyazhnyy
2个回答

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.

所以你可以使用花括号扩展来生成name=value对:
alias {at,cart,cst}='/bin/cat'

所以:
$ 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'

话虽如此,你可以了解一下 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

If you press y when the shell asks you if you want to correct a word, it will be corrected. If you press n, it will be left alone. Pressing a aborts the command, and pressing e brings the line up for editing again, in case you agree the word is spelled wrong but you don't like the correction.


我觉得你不能一次性分配多个别名。 但是你可以像这样循环遍历一个列表:
for a in cart xat vat xst cst vst dog; do alias "$a"='/bin/cat'; done

确保别名没有被其他程序(比如你的例子中的at)使用。

谢谢你的回答,它很有效。我有一台Linux机器,默认使用tsch作为shell。我尝试了以下命令:foreach x ( cst cart );alias $x='/bin/cat';end. - Josef Klimuk
2@JosefKlimuk:听起来值得单独回答。 :-) - David Foerster
@David Foerster,我应该把它作为一个独立的问题来问吗? - Josef Klimuk
2@JosefKlimuk:不,我的意思是你应该根据你之前的评论给这个问题写一个恰当的答案。 - David Foerster