Bash – export script function

bash

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…

Best Answer

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.

Related Question