如何将bash函数与fish一起使用

问题描述 投票:0回答:4

我有一些 bash 函数,比如

#!/bin/sh

git-ci() {
    ...
}

当我不使用鱼时,我的

source ~/.my_functions
中有一条
~/.bash_profile
线,但现在不起作用了。

我可以将 bash 函数与 Fish 一起使用吗?或者唯一的办法就是把它们翻译成鱼的,然后通过

funcsave xxx
保存?

bash fish
4个回答
4
投票

正如@Barmer所说,鱼并不关心兼容性,因为它的目标之一是

健全的脚本

fish 是完全可编写脚本的,其语法简单、干净且一致。你再也不会写 esac 了。

鱼人们认为 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

然后将脚本文件放在路径中的某个位置。现在你可以用鱼来称呼它们了。

希望有帮助。


3
投票

fish
中定义函数的语法与 POSIX shell 和
bash
有很大不同。

POSIX 功能:

hi () { 
    echo hello
}

翻译为:

function hi
    echo hello
end

脚本语法还存在其他差异。有关示例,请参阅 Fish - 友好的交互式 shell 中标题为 Blocks 的部分。

所以基本上不可能尝试在

bash
中使用为
fish
编写的函数,它们与
bash
csh
一样不同。您必须检查所有函数并将它们转换为
fish
语法。


2
投票

如果您不想更改所有语法,一种解决方法是简单地创建一个运行 bash 脚本并传递参数的 Fish 函数。


示例

如果你有这样的功能

sayhi () { 
    echo Hello, $1!
}

您只需通过剥离功能部分来更改它,并将其另存为可执行脚本

echo Hello, $1!

然后创建一个调用该脚本的 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!

0
投票

有一个程序 babelfish 可以将 bash 脚本翻译为 Fish。

例如你的脚本

git-ci() {
echo "Hello"
}

翻译为

function git-ci
  echo 'Hello'
end
© www.soinside.com 2019 - 2024. All rights reserved.