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

BashFunctionShell

Bash Problem Overview


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

Bash Solutions


Solution 1 - Bash

You can export functions. In your ~/.bashrc file after you define the function, add export -f functionname.

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

export -f hello

Then the function will be available at the shell prompt and also in other scripts that you call from there.

Note that it's not necessary to export functions unless they are going to be used in child processes (the "also" in the previous sentence). Usually, even then, it's better to source the function into the file in which it will be used.

Edit:

Brackets in Bash conditional statements are not brackets, they're commands. They have to have spaces around them. If you want to group conditions, use parentheses. Here's your function:

function coolness() {

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

A better way to write that conditional is:

    if [[ -z "$1" || -z "$2" ]]; then

because the double brackets provide more capability than the single ones.

Solution 2 - Bash

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

Solution 3 - Bash

Include in your script the line

source .bashrc

try with the source construct it should work!

Solution 4 - Bash

Any changes made to .bashrc will only take effect in a new terminal session. If you want to apply the changes to your current terminal session, you have to instruct the shell to re-read the .bashrc. The shortest way to to this is to use the . command, which is a synonym to source:

[user@linuxPc]$ . ~/.bashrc

Solution 5 - Bash

$ source .bashrc

Solution 6 - Bash

It's weird; my fuctin won't terminate untill I pass it to another bash instance, like this:

bash myfunction

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionles2View Question on Stackoverflow
Solution 1 - BashDennis WilliamsonView Answer on Stackoverflow
Solution 2 - BashCascabelView Answer on Stackoverflow
Solution 3 - BashLopocView Answer on Stackoverflow
Solution 4 - BashmihaiView Answer on Stackoverflow
Solution 5 - BashPiedPiperView Answer on Stackoverflow
Solution 6 - BashLerian AcosenossaView Answer on Stackoverflow