Search code examples
performanceshellzshrc

How to determine which scripts in my ~/Shell directory are causing my ~/.zshrc file to take so long to be loaded?


I have a ~/.zshrc file that executes all the top-level shell scripts in my ~/Shell directory, these top-level scripts in turn execute the lower-level scripts. For example, scripts in ~/Shell/programs (the so called "lower-level scripts") are executed by the top-level ~/Shell/09-programs.sh script. I have noticed lately that running ~/.zshrc has become much slower than it used to be and I would like to know if there are any methods to determine which script (or set of scripts) in my ~/Shell directory are causing it to become so slow. It would also be helpful to know the precise line number in each script that causes the script to become so slow. If you would like to see my ~/.zshrc file and ~/Shell scripts here is their GitHub repository that I update regularly.


Solution

  • PS4=':[%*]:%x:%I+' zsh -x -l -i
    

    will run a new login shell with each line executed preceded by a second-accurate timestamp, the filename and line number from which it was defined.


    Incidentally, the bash equivalent to this looks something like:

    PS4=':[\t]:$BASH_SOURCE:$LINENO+' bash -x -l -i
    

    ...though personally, I might use $SECONDS in that case rather than \t, to get an integer number of seconds since shell invocation.