Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

My .bashrc had some code that was repetitive so I used a function to simplify it

do_stuff() {
  local version=$1
  export FOO_${version}_X="17"
  export FOO_${version}_Y="42"
}

do_stuff '5.1'
do_stuff '5.2'

However, now when I use my shell the "do_stuff" name is in scope so I can tab-complete and run that function (potentially messing up my environment variables). Is there a way to make "do_stuff" visible only inside the .bashrc?

share|improve this question

2 Answers 2

up vote 11 down vote accepted

Use unset as last line in your .bashrc:

unset -f do_stuff

will delete/unset the function do_stuff.

To delete/unset the variables invoke it as follows:

unset variablename
share|improve this answer
    
Is unset really the only way here? Well, I guess if I use a really long and ugly name for my function the chance of it clashing with something else won't be that big... –  hugomg Dec 11 '13 at 21:21
    
If that's the problem check frist if the function exists: if type do_stuff >/dev/null 2>&1; then echo "exists"; else echo "dont"; fi. –  chaos Dec 11 '13 at 21:30
    
This won't work if do_stuff is used inside another function. When you call that function, you will get bash: do_stuff: command not found. I know this isn't the exact case the op was talking about but it's worth pointing out. –  Burhan Ali Sep 23 at 13:58

The other option is to use underscores like in other scripted languages to indicate you do not intend for this function to be public. The likelihood of you typing _do_foo is pretty small and also unlikely to conflict with anyone else.

share|improve this answer
    
Yes, do use "_" to indicate intent to have a private function, but this has nothing to do with conflicts. Anyone can need "_debug()" or "_safe_copy()". If there is something that helps avoid conflicts, it has nothing to do with underscores. –  Alois Mahdal Mar 7 at 13:49

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.