Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Add Executable Files to $PATH with Bash

    Cameron NokesCameron Nokes
    bashBash

    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.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    Instructor: 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.

    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.

    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.

    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.

    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.

    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.

    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."

    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.

    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.

    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.

    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.

    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.

    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.

    Discuss

    Discuss