Add Executable Files to $PATH with Bash

Cameron Nokes
InstructorCameron Nokes
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 5 years ago

PATH is a global environment variable that represents a list of directories bash looks in for executable files. The executable files for bash commands like grep are all somewhere on your OS’s PATH. We can add our own folders to PATH to make our executables available as a command to bash. In this lesson we’ll learn how to add a new folder to our PATH in .bash_profile and how to symlink an executable file into /usr/local/bin, which is in PATH by default.

Note that in zsh, when modifying your PATH you to provide an absolute path, ~ is not expanded.

Instructor: [00:00] Path is an environment variable that is a list of directories Bash looks in for executable files. If I do Echopath, I can see that lists, and so here's a folder and then this is delimited by colons, and there's another folder.

[00:16] If we run which grip, for example, so tell us that the grip commands executable file is located in the user bin folder which we can see is up here. All the commands that are available on Bash have to be in one of these folders in your path to work.

[00:30] Let's say that I've written a script that I want to make usable everywhere I am in Bash. Instead of, for example, putting it in my home directory and always executing it via an absolute path, I can put the folder my script is in in my path which will allow me to execute it like any other Bash commands.

[00:45] There are two common ways to do this on Mac OS and Linux. The first way to do this is by modifying our path environment variable in Bash profile. If I open Bash profile in my code editor, so I'm going to export pass, and I'm going to reference the path's variable again, because I don't want to overwrite it or anything.

[01:05] I basically want to take everything that's already in my path and just appends to the very end of it. We'll do a colon. I'm going to put my folder that says my home directory, and I'm going to create a folder called "my scripts" here. The executable that I ultimately want in my path will live in the "my scripts" folder.

[01:22] Note, that we're using export here because we want the changes to our path variable to be visible, and be in effect for all sub shells of our current shell. For example, if we invoke the script that then invokes another script increasing your process every time.

[01:36] I wouldn't want the changes in our paths to be visible to all those child processes. This is sort of just a best practice thing. OK, so see that, let's jump back here, so let's do [inaudible] my scripts. And then I'm going to echo how a really basic script into it. I'm going to call it "hello."

[01:57] Notice, I don't have a .sh extension or anything, just because the file name is going to be the name of the command that's executed, and I want the command to be called just "hello," no extension. Then we're going to add excusable permissions to it with the change of mode plus X commands.

[02:16] Now we just see the source Bash profile again, and then if we run "hello," we can see that that works. We Echopath, we can see right here at the end this is the "my scripts" folder that I added. Awesome. That's working. Another way to add an executable to PATH is by Sim Linking an executable file into an existing folder that is always in PATH.

[02:37] Let's see how we do that. Let's create a second scripts. I'm going to call mine "hello 2," and I'm going to do that so that is where I'm right here in my home directory. That's in my home directory. It's not in my path, and I'll give that executable permissions.

[02:56] Now what we're going to do is we're going to Sim Link this into our user local bin. If you remember here in PATH, our user local bin. Here's our first one. That's a good spot for executables for our user. I'm going to run a link commands which is LN.

[03:08] I'm going to pass the S flag which tells us to create symbolic link. A Sim Link or symbolical link is basically a special file that points to the location of a different file. It doesn't really have any contents of its own. It's just a pointer to another file on your hard drive.

[03:23] This is the target. This is what's going to be Sim Linked, and note that I have to have an absolute path here or it's not going to work. Then I'm going to put that in user local bin. OK, so we run that's, that will work off the bat without any kind of sourcing Bash profile or anything.

Shripada Hebbar
Shripada Hebbar
~ 5 years ago

Next time you echo your path, try this: echo $PATH | tr : '\n' | cat

Cameron Nokes
Cameron Nokesinstructor
~ 4 years ago

Note: starting with Catalina, macOS now uses zsh as the default shell instead of bash. zsh and bash are very similar, but zsh looks for a .zshrc and .zprofile instead. See for more info on the differences between the two.

Markdown supported.
Become a member to join the discussionEnroll Today