All PATHs lead to…

I’d asked the question recently as to what happens if you add lots of items to your PATH variable in Bash, Z shell or your other favourite shell of choice. I didn’t find any straight answers, so I thought I’d experiment.

The reasoning behind the question comes from the way I have my shell configured. I’ve a number of script on Github which are duplicated in my own “dotfiles” type repository. I wanted to do away with this duplication, and I also wanted to avoid symlinking stuff into a main bin directory.

The ideal solution is to just have each of my GitHub repos checked out and added to my PATH environment variable, but I wondered if having a large number of paths would slow things down.

So, experiment time!

#!/bin/bash

# Standard bash command to make a temporary directory.
MKTEMP_DIR_CMD='mktemp -d';

# Number of items to add to PATH
COUNT=1000

# Choose the correct commands for non-standard platforms
function get_platform_cmds() {

        # Mac
        if [ $(uname) == 'Darwin' ]; then
                MKTEMP_DIR_CMD="mktemp -d ${TMPDIR}tmp.XXXXXXXXXX"
        fi

}

get_platform_cmds

# Create executable files in their own directories and those directories to PATH
for i in $(seq 1 $COUNT); do
        dir=$($MKTEMP_DIR_CMD)
        filename=$(echo $dir | sed 's|^.*/||g')
        echo "${dir}/${filename}"
        cat << EOF > ${dir}/${filename}
#!/bin/bash
echo 'Test'
EOF
        chmod +x ${dir}/${filename}
        PATH=${PATH}:${dir}
done

echo ''

# Export the modified PATH variable for the subshell
export PATH

# Run a new subshell without sourcing configs, so we don't override PATH
/bin/bash --noprofile --norc

exit $?

This script just creates 1000 temporary directories, with a uniquely named executable file in each of them. It then adds them to the PATH variable and exports it for a subshell.

So, with 1000 extra directories in my path, were things any slower? Not that I could tell, and in practice I’d be surprised if I went above adding 20 new directories.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: