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 godbyk's answer, 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. # have() { 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 && have="yes" }
This checking spends most of the time in the whole progress.
godbyk's solution is to 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 if [ -f /usr/local/etc/bash_completion ]; then source /usr/local/etc/bash_completion fi # 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 to reduce search path list. The total time would be very small, even after I removed the workaround for type
.