Welcome to another episode of Linux notes. Today, I'll briefly talk about adding stuff to Path on POSIX[1] systems.
So the PATH is a very long colon delimited string that gets extended whenever you add a new directory to it. It looks something like this:
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/bin:/usr/local/sbin
The PATH string above is for a Linux computer and it has 7 different directories within it. The first directory of this PATH string (/usr/local/bin
) is where user-installed executables that are not part of the core system are located. The second directory (/usr/bin
) contains system-wide executables like cd, grep, cat, find, etc. Other directories contain different types of executables too. You can read up on them here.
When you add a folder to path, you're telling the OS that it can find executables in that folder. Say for example you have an executable file, htmlformatter
, that you developed for formatting your HTML files. This executable in located in the folder /User/MacUser/Documents/code-formatters/
. If you add this folder to path, you would be able to call htmlformatter as you would any other normal executable such as cat, find, or grep.
To add this Executable to PATH, just add the line below to the end of the shell configuration file (.bashrc, .zsh_profile, etc) of your active shell. $PATH variable in your shell contains the must updated PATH string, so this export re-sets the variable, essentially extending the PATH string. So yeah, copy the line below and paste it in your shell config file.
export PATH="/User/MacUser/Documents/code-formatters/:$PATH"
After adding and saving the line above, source the shell configuration file so that the PATH variable is updated in your current session. You can source a shell config file like so:
source ~/.bashrc # I'm assuming bash is your current active shell
# some nerds prefer using dot (.) Sigh! 😔
. ~/.bashrc
And voila! You'll be able to call htmlformatter from your current active shell or any new shell[2].
-
A fun project might be to add a link to a HTML formatter and give step by step instructions to add that executable to path. I mean, this will be beneficial to complete noobs, yeah?
-
What happens when an executable with the same name but different functionality exists in both /usr/bin and /usr/local/bin? 😬😬😬
[1] Or UNIX? What's the difference?
[2] You can also call the htmlformatter executable in other open shells by sourcing the ~/.bashrc file in those shells.