[英]Alias Substitution in Fish Shell
Question: Is there a Fish equivalent of Bash's alias substitution or what's the recommended best practice to keep the code clean and DRY? 问题:是否存在类似于Bash别名替换的Fish或者保持代码清洁和干燥的建议最佳做法是什么?
Background: There's a very useful feature of aliases in Bash called alias substitution. 背景: Bash中有一个非常有用的别名特征叫做别名替换。 It's mentioned briefly in the man page:
它在手册页中简要提到:
alias [-p] [name[=value] ...]
...
A trailing space in value causes the next word to be checked for alias substitution when the alias is expanded.
...
The power of this functionality may easily be conveyed with an example. 通过示例可以容易地传达该功能的强大功能。 Consider that many users define a grep alias.
考虑到许多用户定义了一个grep别名。 Here's mine:
这是我的:
# extended regex, skip binaries, devices, sockets, & dirs, colored, & line
# -buffered. use a non- canonical alias instead of GREP_OPTIONS which may wreck
# poorly written scripts
alias g='grep -EID skip -d skip --color=auto --line-buffered'
Similarly, many of those same users define an alias for xargs. 同样,许多相同的用户为xargs定义了一个别名。 Here's mine without alias substitution:
这是我的没有别名替换:
alias x='xargs -rd\\n' # \n delimited, don't run on empty in
And finally, here's how I might want to use it but it doesn't work: 最后,这是我可能想要使用它但它不起作用:
$ find|x g foo
xargs: g: No such file or directory
This command fails because x is expanded to xargs and it can't find an executable called g
. 此命令失败,因为x已扩展为xargs,并且找不到名为
g
的可执行文件。 There are a number of workarounds for this but I think most are awful. 有很多解决方法,但我认为大多数都很糟糕。 However, by just adding a trailing space, the shell will perform alias substitution on our behalf and the command will work as intended:
但是,通过仅添加尾随空格,shell将代表我们执行别名替换,命令将按预期工作:
alias x='xargs -rd\\n ' # \n delimited, don't run on empty in, + expansion
# ^-- this space is for expanding a subsequent alias
Please keep in mind that this is just an example , not necessarily the actual use case. 请记住,这只是一个例子 ,不一定是实际的用例。
Update 2015-05-06 更新2015-05-06
I never found a Fishism solution but I felt the alternative was worth a comment. 我从未找到过Fishism解决方案,但我觉得替代方案值得评论。 I took the approach of creating shell scripts in
~/bin
. 我采取了在
~/bin
中创建shell脚本的方法。 The downsides are: 缺点是:
However, I felt the upsides were pretty huge: 但是,我觉得好处很大:
This isn't a Fish based solution -- but I suspect the fish answer is going to be it's not possible . 这不是基于鱼类的解决方案 - 但我怀疑鱼的回答是不可能的 。
You could create your aliases as .fish
or .sh
scripts and symlink them to /usr/local/bin
-- this will give you the equivalent behaviour. 您可以将别名创建为
.fish
或.sh
脚本,并将它们符号链接到/usr/local/bin
- 这将为您提供等效行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.