Every time I start a new session in my macOS, it will take a lot of time to initialize the session. After some survey, the key problem is the bash_completion.

A Q&A could be found here.

According to the answer of godbyk, it seems like the problem is the 'have' function:

# This function checks whether we have a given program on the system.
# No need for bulky functions in memory if we don't.
    unset -v have
    # Completions for system administrator commands are installed as well in
    # case completion is attempted via `sudo command ...'.
    PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin type $1 &>/dev/null &&

This checking spends most of time in the whole progress.

godbyk's solution is change this file and comment the "PATH=.... type", and it will always return true.

This solution works!

However, changing a configure file managed by brew is not a good idea for me. My solution is:

# Add a function type, which will always return true
function type() { return 0 ; }
# Load bash_completion
[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion
# Remove this function type
unset type

Here is my test:

yu@algac:~$ time . /usr/local/etc/bash_completion

real    0m4.222s
user    0m0.184s
sys 0m0.055s
yu@algac:~$ function type() { return 0 ; }
yu@algac:~$ time . /usr/local/etc/bash_completion

real    0m0.193s
user    0m0.155s
sys 0m0.034s

The time costing reduced from 4.222s to 0.193s!

How could it come? It worked pretty fine in other's machine.

To reproduce this situation, I checked a lot of factors. It seems like the key problem is the "PATH" environment.

I changed the PATH environment, and cleaned up the search path options. The total time still reduced very quick, even after I removed the workaround for 'type'.

Categories: Code


Ideals are like the stars: we never reach them, but like the mariners of the sea, we chart our course by them.

Leave a Reply

Your email address will not be published. Required fields are marked *