Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

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.

Question:

  • Is there any way to hide fun1 and fun2, and prevent them from being called outside from script.bash.
share|improve this question
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

3 Answers 3

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

share|improve this answer
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

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.

share|improve this answer

Script:

unset my_protected_function
function my_protected_function() {
    echo "function executed"
}
[ "$0" != "/bin/bash" ] && unset my_protected_function
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?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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