如何对 fish 使用 bash 函数
How to use bash functions with fish
我有几个bash函数,比如
#!/bin/sh
git-ci() {
...
}
当我不使用 fish 时,我的 ~/.bash_profile
中有一条 source ~/.my_functions
行,但现在它不起作用。
我可以将 bash 函数用于 fish 吗?或者唯一的方法是将它们翻译成鱼然后通过 funcsave xxx
?
保存它们
fish
中定义函数的语法与 POSIX shell 和 bash
.
有很大不同
POSIX函数:
hi () {
echo hello
}
翻译为:
function hi
echo hello
end
脚本语法还有其他差异。有关示例,请参阅 Fish - The friendly interactive shell 中标题为 Blocks 的部分。
所以基本上不可能尝试在fish
中使用为bash
编写的函数,它们与bash
和csh
一样不同。您必须检查所有函数并将它们转换为 fish
语法。
正如@Barmer 所说,fish 不关心兼容性,因为它的目标之一是
Sane Scripting
fish is fully scriptable, and its syntax is simple, clean, and consistent. You'll never write esac again.
鱼友们认为 bash 疯了,我个人同意。
您可以做的一件事是将 bash 函数放在单独的文件中,并在 fish 中将它们作为函数调用。
示例:
之前
#!/bin/bash
git-ci() {
...
}
some_other_function() {
...
}
之后
#!/bin/bash
# file: git-ci
# Content of git-ci function here
#!/bin/bash
# file: some_other_function
# Content of some_other_function function here
然后将脚本文件放在路径中的某个位置。现在你可以从鱼中调用它们了。
希望对您有所帮助。
如果您不想更改所有语法,一种解决方法是简单地创建一个 fish 函数,该函数 运行 是一个 bash 脚本并直接传递参数。
例子
如果你有这样的功能
sayhi () {
echo Hello, !
}
您只需通过剥离函数部分来更改它,并将其另存为可执行脚本
echo Hello, !
然后创建一个调用该脚本的 fish 函数(例如,名称为 sayhi.fish
)
function sayhi
# run bash script and pass on all arguments
/bin/bash absolute/path/to/bash/script $argv
end
而且,瞧,只是 运行 像往常一样
> sayhi ivkremer
Hello, ivkremer!
我有几个bash函数,比如
#!/bin/sh
git-ci() {
...
}
当我不使用 fish 时,我的 ~/.bash_profile
中有一条 source ~/.my_functions
行,但现在它不起作用。
我可以将 bash 函数用于 fish 吗?或者唯一的方法是将它们翻译成鱼然后通过 funcsave xxx
?
fish
中定义函数的语法与 POSIX shell 和 bash
.
POSIX函数:
hi () {
echo hello
}
翻译为:
function hi
echo hello
end
脚本语法还有其他差异。有关示例,请参阅 Fish - The friendly interactive shell 中标题为 Blocks 的部分。
所以基本上不可能尝试在fish
中使用为bash
编写的函数,它们与bash
和csh
一样不同。您必须检查所有函数并将它们转换为 fish
语法。
正如@Barmer 所说,fish 不关心兼容性,因为它的目标之一是
Sane Scripting
fish is fully scriptable, and its syntax is simple, clean, and consistent. You'll never write esac again.
鱼友们认为 bash 疯了,我个人同意。
您可以做的一件事是将 bash 函数放在单独的文件中,并在 fish 中将它们作为函数调用。
示例:
之前
#!/bin/bash
git-ci() {
...
}
some_other_function() {
...
}
之后
#!/bin/bash
# file: git-ci
# Content of git-ci function here
#!/bin/bash
# file: some_other_function
# Content of some_other_function function here
然后将脚本文件放在路径中的某个位置。现在你可以从鱼中调用它们了。
希望对您有所帮助。
如果您不想更改所有语法,一种解决方法是简单地创建一个 fish 函数,该函数 运行 是一个 bash 脚本并直接传递参数。
例子
如果你有这样的功能
sayhi () {
echo Hello, !
}
您只需通过剥离函数部分来更改它,并将其另存为可执行脚本
echo Hello, !
然后创建一个调用该脚本的 fish 函数(例如,名称为 sayhi.fish
)
function sayhi
# run bash script and pass on all arguments
/bin/bash absolute/path/to/bash/script $argv
end
而且,瞧,只是 运行 像往常一样
> sayhi ivkremer
Hello, ivkremer!