DEV Community

Steve
Steve

Posted on

Is there a good explainer for bash profiles and paths?

I've recently started playing with Jekyll and other Ruby static site generators, which has resulted in me needing to add things to my path. I realised that I didn't really know what that meant or whether I was doing it correctly, therefore I'm looking for a good explainer on bash profiles and paths.

Thanks in advance!

Top comments (3)

Collapse
 
_morgan_adams_ profile image
morgana

Lots of resources online if you want to go deeper on those, but here are some basics.

PATH is where your shell looks to find commands to run. You can look at that more by running the command

echo $PATH

You'll see several ":" separated paths. If you run ls against one of those directories e.g. /usr/bin, you'll see a lot of entries. All of those are commands your shell knows about because of the PATH.

You can add to your path if you need your shell to look in additional locations, usually this is done with a bash profile.

A bash profile is effectively a simple bash script to help set up your shell environment. One of the things bash profiles commonly do is configure your PATH.

Each user can configure their own ~/.bash_profile for their own configurations. This file is executed when you log in. Additionally there are ~/.bashrc files that serve a similar purpose. These are executed when you start an interactive shell. That might be confusing, but here's what I think is a good explanation, there are many others out there quora.com/What-is-bash_profile-and...

Looking at the Jekyll Ubuntu install instructions (jekyllrb.com/docs/installation/ubu...) it looks like the gems are installed under your HOME directory. HOME is one of those variables set up in your environment when you log in. Check out it's content:

echo $HOME

You'll probably see something like /home/myusername. Which means that that Jekyll commands are being stored in /home/myusername/gems/bin. HOME is pretty universal though so the location is pretty much the same for everyone if you just use $HOME/gems/bin. bin is a common folder name for a folder with executable commands. This particular path though isn't part of your PATH so you have to add it to your personal bash_profile.

If you've already followed the Jekyll instructions, you'll probably have to log back in to see the $HOME/gems/bin path added to your PATH environment variable (note the instrucitons use bashrc vs bash_profile). Alternatively you could just run

source ~/.bashrc

Which is a way of re-executing your .bashrc file to set up your environment again. (Often your ~/.bash_profile will contain this source command to your bashrc file to ensure both are run when you log in).

Hope that was useful.

Collapse
 
ferricoxide profile image
Thomas H Jones II • Edited

Bear in mind that multi-element environment variables are processed left-to-right. Which is to say, if you have, say, three commands (shared-libraries, man pages, etc.) that share a common name, the one that appears left-most in the relevant environment-variable is the one that will get invoked. Get familiar with tools like which, ldd, locate and the like. They can help you figure out why things are acting unexpectedly when you invoke a given binary or link to a given library or try to read a given man-page/infodoc/etc.

As to the various profiles... There's rules to how early and in what order they're read and under what circumstances they're read. And, you'll become acutely aware of these rules when you're pulling your hair out wondering "why does this work from my SSH session but doesn't work from a cron-job or a systemd unit??"

Collapse
 
stevenjmesser profile image
Steve

Thank you, that’s great! Exactly what I was looking for too. What I wasn’t clear on was where stuff was being installed and what the PATH did, but you really cleared it up.

Thanks again!