3

I am trying to implement the solution found here. However, a coworker suggested that instead of adding functions directly to a .bash_profile or .bashrc file that instead, the custom scripts be put in their own file and soft linked in /usr/bin. I've done this (and made the script executable), but it doesn't seem to work. I think the functions have to be exported, but I'm not sure how.

How do I export the custom script functions so that they will be on the path? eg: So I can run them like any other command.

EDIT #1:

I've found this (and tried it), but it still doesn't seem to work, I'm not sure what I'm doing wrong now...

3 Answers 3

6

There's two totally separate things at work here. Shell scripts are executable files you can run directly, so if you made the file /usr/bin/foo (you shouldn't manually put stuff there, but anyway), you would be able to run foo and it would execute that:

$ cat /usr/bin/foo
#!/bin/bash
echo One

$ foo
One

Functions are also executable by name, but they need to be defined first, which happens when you type them directly into a shell, or source the file that contains them with the source or . commands:

$ function bar() {echo Two;}

$ bar
Two

$ cat functions.sh
function baz() {echo Three;}

$ baz
command not found: baz

$ . functions.sh

$ baz
Three

Defining functions in files that are on your path won't do anything; you still need to source those files in a login script. If you want to make separate scripts you might as well just name them the same as their functions and get rid of the function entirely, so you can just call the script by name

4

A script can't push definitions up to you. You have to bring them in from the script by sourcing them. This is done by . /usr/bin/cdjs (if the script was in /usr/bin/cdjs). After that, you can use cdjs anytime.

Your co-worker may have been suggesting making the functions scripts instead. The cdjs function can be made a script, but in order for it to work, you have to source the script every time you use it, like this: . cd2js.

To do that, you would have a file /usr/bin/cd2js which is executable and contains:

$/bin/sh
cd /domains/"$1"/applications/j2ee-apps

If you frequently do a limited number of things in that directory, you might want to make several scripts that does those things for you.

So you can have a script like this:

#!/bin/sh

ls /domains/"$1"/applications/j2ee-apps/

And put it at /usr/bin/lsj2. Then you can just do lsj2 some-domain-1 anytime to see the contents.

0

Maybe I need to put each function in it's own file with the function name as the file name and break the code out of the functions? I would think I could have a single script with multiple functions exported to the path...

2
  • Did you intend this to be a comment on the question? Oct 10, 2011 at 19:18
  • No, it was sort of a half solution. I think I was on the right track with this, but had a few pieces missing. Thanks. Oct 10, 2011 at 21:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .