1

Alright, so let's say I have a .bash file with this inside of it

lib.bash

#!/bin/bash
function hello_world {
     echo "Hello World!"
}

That file won't be called on it's own, instead it'll be called via another bash file, i.e

Startup.bash

#!/bin/bash
bash lib.bash
hello_world

But, if I run Startup.bash I get the error: hello_world: command not found

What am I doing wrong, or is it not possible to do what I'm trying to do on bash.

1
  • 2
    You can try source lib.bash Commented Sep 3, 2013 at 11:17

3 Answers 3

2

You can use this command in your startup.bash:

source lib.bash

the source command runs the file in the current shell environment, unlike using bash lib.bash
(or . lib.bash) which creates a new, separate environment for that script (and only that script) and is why the function is not carried over.

(source)

Sign up to request clarification or add additional context in comments.

1 Comment

Source works, the reason why I hadn't used it before is since every time I used it in what I'm truly using it for; it didn't work. But, it turns out it was an error on my part that caused it to not work, so it does now. Thanks!
1

why don't you call the function directly inside of the first script?

It would look something like this:

#!/bin/bash
function hello_world {
     echo "Hello World!"
}
hello_world

If it is a simple script, shouldn't be a problem at all. Otherwise try the source command, like minerz029 suggested :)

1 Comment

Because I wanted the function to be independent from the main file.
0

See if this could be helpful to you as well:

Shell Script Loader

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.