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.

I have a build script that can change what binaries are in my $PATH (it doesn't edit $PATH itself, but it adds/deletes files to folders that are already in $PATH). zsh's autocompletion doesn't update to reflect the files that have been added/deleted in response to the build, so I have to either restart the shell or execute the 'rehash' shell built-in to tell it to update its autocomplete cache.

I decided I wanted to automate running rehash so at first I did this:

build() {
    ~/bin/real_build && rehash
}

But shell functions run in their own subshell, so rehash would update the cache for the subshell, instead of the shell in the terminal I was using. So I switched to this:

alias build='~/bin/real_build && rehash'

Which works, except when I want to pass arguments to build. If I pass 'clean' I'd like it to execute:

~/bin/real_build clean && rehash

But instead it will do:

~/bin/real_build && rehash clean

So I'm stuck -- I can use an alias and not be able to pass arguments, or I can use a function and not have it actually update the autocomplete entries. Is there a way to make both work?

share|improve this question
    
I may be wrong because I've never used zsh before today, but calling rehash from a function does seem to work for me. –  lk- Aug 17 '12 at 15:58
add comment

2 Answers 2

Shell functions do not run in their own subshell, unless you use parentheses instead of the usual curly brackets around the body. Your function does update the executable hash on every version of zsh that I've seen, and I don't think there's an option to change this behavior.

share|improve this answer
add comment

I think source might be the best option here. Here is an example:

cat << EOF > foo
touch $1
chmod +x $1
rehash
EOF

Execute foo like this:

source foo bar

If this was in your path, bar is now completable.

share|improve this answer
add comment

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.