如何从外壳中调用.bashrc中定义的函数? [英] How do you call a function defined in .bashrc from the shell?

查看:82
本文介绍了如何从外壳中调用.bashrc中定义的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的.bashrc中,我有一个名为hello的函数:

In my .bashrc, I have a function called hello:

function hello() {
   echo "Hello, $1!"
}

我希望能够从外壳中按如下方式调用hello():

I want to be able to invoke hello() from the shell as follows:

$ hello Lloyd

并获得输出:

> Hello, Lloyd!

诀窍是什么?

(我想到的真正功能当然更复杂.)

(The real function I have in mind is more complicated, of course.)

我认为,这实际上是由于函数中的语法错误引起的! :(

This is REALLY caused by a syntax error in the function, I think! :(

function coolness() {

    if[ [-z "$1"] -o [-z "$2"] ]; then
        echo "Usage: $0 [sub_package] [endpoint]";
        exit 1;
    fi
        echo "Hi!"
}

推荐答案

函数中的测试不起作用--z子句不应带有方括号,if和方括号之间应有一个空格.它应显示为:

The test in your function won't work - you should not have brackets around the -z clauses, and there should be a space between if and the open bracket. It should read:

function coolness() {

    if [ -z "$1" -o -z "$2" ]; then
        echo "Usage: $0 [sub_package] [endpoint]";
        exit 1;
    fi
    echo "Hi!"
}

这篇关于如何从外壳中调用.bashrc中定义的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆