90

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!"
}
5
  • 1
    You should be able to call functions defined in the .bashrc from the shell, since it should be sourced by your shell. Is yours not?
    – Cascabel
    Commented Sep 30, 2009 at 20:40
  • 5
    Ah, you mean you want to call it from a script, not from the interactive shell? Might want to update your question to reflect this so others can find it. (The bashrc is not generally sourced for non-interactive shells.)
    – Cascabel
    Commented Sep 30, 2009 at 20:42
  • As it is, the title is highly misleading -- it doesn't line up with the actual question you meant to ask, and thus doesn't line up with the answer you accepted. That makes it tricky to use this to help other people with related questions. Commented Oct 5, 2017 at 20:39
  • Have a look at the bash man-page, section INVOCATION, to see under which condition .bashrc is processed by your shell. Commented Nov 28, 2023 at 16:10
  • Your function coolness is of course not syntactically correct. You can track down such issues, by pasting it into shellcheck. Commented Nov 28, 2023 at 16:12

5 Answers 5

136

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.

2
  • -o isn't valid in [[ ]]. || should be used there instead. Commented Oct 5, 2017 at 20:37
  • Thanks, that works. And we don't need to put brackets in the end when calling the function.
    – Liker
    Commented Apr 20, 2022 at 12:41
35

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!"
}
3
  • 1
    Small note, exit 1 might close your terminal, you won't see the Usage anyway. You might want to use return 1 instead. Commented Dec 7, 2016 at 7:05
  • -o is flagged obsolescent in the POSIX test standard. The proper way to write that is [ -z "$1" ] || [ -z "$2" ]. Commented Oct 5, 2017 at 20:36
  • Can it be accessed from another file like run.sh file?
    – alper
    Commented Jul 25, 2021 at 13:59
21

Include in your script the line

source .bashrc

try with the source construct it should work!

1
  • 3
    it's the if statement that won't behave! (i'm a beast at java, but bash beats the hell out of me)
    – les2
    Commented Sep 30, 2009 at 20:53
18

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
2
  • 1
    or . ~/.bashrc which will work regardless of your current directory. Commented Sep 25, 2017 at 8:21
  • Can we do it with different filename like: . ~/.global_functions?
    – alper
    Commented Jul 25, 2021 at 14:00
5
$ source .bashrc

Not the answer you're looking for? Browse other questions tagged or ask your own question.