简体   繁体   English

如何一次将参数传递给bash和fish中的别名/函数

[英]How to pass argument to alias/function in bash and fish at once

My question is very similar to: How to pass command line arguments to a shell alias? 我的问题非常类似于: 如何将命令行参数传递给Shell别名?

I want to create alias that will accept arguments. 我想创建将接受参数的别名。 Guys in above question suggested functions and indeed this is solution. 以上问题的人建议了功能,而实际上这是解决方案。

However, I am using at once fish (Friendly Interactive SHell) and bash. 但是,我立即使用鱼(Friendly Interactive SHell)和bash。 I keep all my custom aliases in single file, that I load no matter if I am using fish or bash. 我将所有自定义别名保存在单个文件中,无论使用鱼还是bash都可以加载。

I know how to create alias/functions in bash, and I know how to create alias/functions in fish. 我知道如何在bash中创建别名/功能,也知道如何在fish中创建别名/功能。 I do not know, how to create alias/function at once for both fish and bash. 我不知道如何为鱼和bash同时创建别名/功能。

It doesn't have to be alias/function (it can be edgy hack), just end effect should work like expected. 它不必是别名/功能(它可以是前卫的技巧),只是最终效果应该像预期的那样起作用。

alias rmi="function _rmi() { docker ps -a | grep $1 | awk '{print $1}' | xargs --no-run-if-empty docker rm -f ; docker images | grep $1 | awk '{print $3}' | xargs --no-run-if-empty docker rmi -f }; _rmi()" 别名rmi =“ function _rmi(){docker ps -a | grep $ 1 | awk'{print $ 1}''| xargs --no-run-if-empty docker rm -f; docker images | grep $ 1 | awk'{print $ 3}'| xargs --no-run-if-empty docker rmi -f}; _rmi()“

Above one is accepted by bash, but not fish. bash可以接受以上一项,但不接受鱼。

function go sudo service $argv restart end 功能转到sudo服务$ argv重新启动结束

Above is accepted by fish, but now bash. 以上被鱼所接受,但现在重击。

alias apts="aptitude search" 别名apts =“ aptitude search”

Plain aliases as above one are accepted by both bash and fish, but argument cannot be inside (must be at very end). bash和fish都接受上面的普通别名,但是参数不能在内部(必须在末尾)。

How to unify it? 如何统一呢?

There is no proper general solution to this, fish is not compatible with bash. 没有适当的一般解决方案,鱼与bash不兼容。

Also, the alias something="function" is unnecessary. 另外, alias something="function"也是不必要的。 You can just define the function directly. 您可以直接定义函数。

If what you wish to execute does not change anything about the shell's internal state, you can make a script instead. 如果您希望执行的操作没有更改外壳内部状态的任何内容,则可以编写一个脚本。

Eg create a file called "rmi" that contains 例如,创建一个名为“ rmi”的文件,其中包含

#!/bin/bash
docker ps -a | grep $1 | awk '{print $1}' | xargs --no-run-if-empty docker rm -f
docker images | grep $1 | awk '{print $3}' | xargs --no-run-if-empty docker rmi -f

somewhere in $PATH (in both bash and fish). $ PATH中的某个位置(包括bash和fish)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM