简体   繁体   English

如何在鱼壳中导出功能

[英]How to export a function in fish shell

I'm porting some of my script from bash to fish shell, and fail to get access to my utilities functions. 我正在将某些脚本从bash移植到fish shell,但无法访问我的实用程序功能。

Bash 巴什

Here is how I do it in bash, first declare my methods in "$HOME/.my-posixrc" : 这是我在bash中的操作方法,首先在"$HOME/.my-posixrc"声明我的方法:

function configure_date_utilities() {
    function today() {
        to-lower "$(date '+%Y-%b-%d')"
    }
    function today-num() {
        to-lower "$(date '+%Y-%m-%d')"
    }
    function now() {
        to-lower "$(date '+%Y-%b-%d-%H:%M')"
    }
}

Then source this file : 然后获取此文件:

source "$HOME/.my-posixrc"

So I can do: 所以我可以做:

$ today

2015-dec-13 2015-DEC-13

Fish

function configure_date_utilities
    function today
        to-lower (date '+%Y-%b-%d')
    end
    function today-num
        to-lower (date '+%Y-%m-%d')
    end
    function now
        to-lower (date '+%Y-%b-%d-%H:%M')
    end
end

Then source this file in ~/.config/fish/config.fish : 然后在~/.config/fish/config.fish该文件:

source "$HOME/.my-posixrc"

But the methods aren't found : 但是找不到方法

$ today

The program 'today' is currently not installed. 当前未安装“今天”程序。 You can install it by typing: sudo apt-get install mhc-utils 您可以通过键入以下命令进行安装:sudo apt-get install mhc-utils

Question

How do I "export" my function so I can access them in my prompt? 如何“导出”函数,以便可以在提示符下访问它们?

PS: my dotfiles are available on github . PS:我的dotfiles在github上可用

Drop the outer function or call it in the file. 删除外部函数或在文件中调用它。

In fish, all functions are global, but your inner functions won't be defined because their definition is never run. 在fish中,所有函数都是全局的,但是不会定义您的内部函数,因为它们的定义永远不会运行。

So either: 所以:

function configure_date_utilities
    function today
        to-lower (date '+%Y-%b-%d')
    end
    function today-num
        to-lower (date '+%Y-%m-%d')
    end
    function now
        to-lower (date '+%Y-%b-%d-%H:%M')
    end
end
configure_date_utilities

or 要么

function today
    to-lower (date '+%Y-%b-%d')
end
function today-num
    to-lower (date '+%Y-%m-%d')
end
function now
    to-lower (date '+%Y-%b-%d-%H:%M')
end

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

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