les2 les2 - 5 months ago 14
Bash Question

How do you call a function defined in .bashrc from the shell?

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

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


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

$ hello Lloyd


And get the output:

> Hello, Lloyd!


What's the trick?

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

EDIT: 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!"
}

Answer

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!"
}