global bash shell functions
Hi, I wonder if there is anyway to make a user-defined bash shell function global, meaning the function can be use in any bash shell scripts, interactively or not.
This is what I attempted: Code:
$ tail -n 3 /etc/bashrc |
you can source it. man source for more
|
That works! Thanks.
|
wait wait...
There is an undesired side-effect of sourcing the file. As you can see, the command 'echotm' displays timestamp when echo'ing. It is widely used in writing logs in my systems. However, as a habit whether good or bad, I have an 'exit' statement in all the scripts. The exit statement down in some sub-script (script called by another) terminates the entire script in the first level, rendering the whole thing broken. Code:
$ cat lv1.sh My last resort is to make 'echotm' a executable shell script file in /usr/local/bin. |
You can also use alias for this purpose
Code:
alias echotm='echo hello world';export echotm |
Hi,
Sourcing is done in the current shell, executing is done in a new child shell. Sourcing a script that you would normally execute can have side effects (exit being one of those). If you put your function in, for example .bashrc or .bash_profile it will be accessible in all the (bash) shells and its sub-shells. I have, for example, this function in my .bashrc: Code:
ALL2LOWER () You can also make a dedicated file that you can source that holds all the functions you need, there shouldn't be a hash-bang or exit in that file. You can decide to source this file by hand when you need it or source this file from for example .bashrc so that all the functions are accessible in all shells. Hope this helps. |
Quote:
Anyway, the solution I think would be to just call the subscript inside a subshell: a.sh Code:
#!/bin/sh Code:
#!/bin/sh Code:
#!/bin/sh ---- EDIT ---- You might also want to try Shell Script Loader. You can use the call() function for that. |
Functions can also be exported in the same way variables can (at least in bash). Just use the -f flag.
export -f functionname This will make it available to any script run within that shell. Overall though, I think it's best simply to incorporate everything directly into your script in some way. The more external dependencies you have, the more difficult it will be to keep track of it all in the long run. |
Quote:
-- Alias won't be a nice option because it has limitations such as, only the front part is converted. Alias can't do things like 'ls -l $@ | less'. -- Also, I did put the function echotm in my .bashrc or /etc/bashrc, and export -f echotm. It works. And then putting common functions in a separate file without hash-bang and exit seem to be working very well too. Thank you all. |
Quote:
Quote:
I somehow remember something similar with this conversation. It was quite an odd event of the past :). |
Quote:
Quote:
|
I did the lv1-lv2-test one machine, and then re-type the output in LQ on another machine, which is why.
---- why odd? Method 1: ~/.bashrc: Code:
# .bashrc Code:
#!/bin/sh Code:
$ sh a.sh ~/common_func: Code:
echo3 () { Code:
#!/bin/sh Code:
$ sh b.sh |
All times are GMT -5. The time now is 03:45 AM. |