I'm writing a simple script script.bash that is supposed to export some variables environment, to use it in my current session I have to source it

$ . script.bash


function fun1() {
    # TODO

function fun2() {
    # TODO

function main_script() {
    # ...
    # ...
    fun1 # calling fun1
    # ...
    func2 $ 1 $2 # calling fun2
    # ...

to use script.bash, call main_script function.


  • Is there any way to hide fun1 and fun2, and prevent them from being called outside from script.bash.
  • 1
    Asking about security is a massive question (one that is pretty much unanswerable with the current info provided), perhaps you could share what specific concerns you may have with security? Things that'll help in a general sense are the ownership/permissions of the script and what it/the owner/group are allowed to do on your system. – nerdwaller May 22 '13 at 22:38
  • @nerdwaller The second question has deleted. – tarrsalah May 22 '13 at 22:51
  • I not a bash programmer, but if you want to limit the accessibility of the functions, and they're not used elsewhere, perhaps encapsulate their definitions inside the main function? – Ƭᴇcʜιᴇ007 May 23 '13 at 14:49

Add unset -f fun1 fun2 at the end of the script.
The -f spesifies function, as unset can unset both functions and variables.

unset man page entry

  • 1
    then, how can I use the functions in my main function ? – tarrsalah May 22 '13 at 22:27
  • Unless I'm missing something: You run the script, the main function runs when you call it, it does what it does; after everything is done with the functions, the last line which unsets is run. Unless you exit the return or break the script in one of the functions, the unset will unset them – demure May 22 '13 at 22:38
  • the script is sourced in .bash_profile , so all the functions are unseted before even calling the main function. – tarrsalah May 22 '13 at 22:48
  • since you are doing it that way, you could add the unset to the bashrc – demure May 22 '13 at 22:49
  • It seems impossible to achieve what I want. – tarrsalah May 22 '13 at 22:53


unset my_protected_function
function my_protected_function() {
    echo "function executed"
[ "$0" != "/bin/bash" ] && unset my_protected_function

When you run it directly, look ma no function

$ ./test.sh
./test.sh: line 15: my_protected_function: command not found

When you source it:

$ source ./test.sh 
function executed

That what you had in mind?


you can mangle the function name, then use 'alias'/'unalias' to bind/unbind it to a human-readable name when you want to use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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