How to Edit/Change the PATH Environment Variables Mac OS X

It’s very trouble-free task to edit/ change the path environment variables on Mac OS X.

The recommended way is by editing your .bash_profile  file.This file is read and the commands in it are  executed by bash every time you log in to the system.The best part is that this file is specific to your user so you won’t affect other users on the same system by changing it.

Step 1: Open up a Terminal window (this is in your Applications/Utilites folder by default).

Step 2: Enter the following command:

              open ~/.bash_profile

This will open the .bash_profile file in TextEdit(the default text editor included on your Mac). This file allows you to customize the environment your user runs in.

Or

   you can enter the following command:

             edit .bash_profile

This will open the .bash_profile in TextEdit or TextWrangler whose contents can be edited as you like.Also you can use

cat .bash_profile which will output the contents of the file directly into the terminal window so that you can review it.

Note: With cat command you cannot edit the file but you can have a look at the contents.

Step 3:Add the following to the end of the file adding whatever additional directory you want in your path:

For instance, export PATH=”$HOME/.rbenv/bin:$PATH”

This example would add ~/.rbenv to the PATH.The $PATH part is very important as it appends the existing PATH to preserve it in the new value.

Step 4: Save the .bash_profile file and quit (Command+Q) TextEdit or TextWrangler in this illustration.

Step 5: Force the .bash_profile to execute.This loads the values immediately without having to reboot.To execute the file ,run the following command in your terminal window.

        source  ~/.bash_profile

That’s it!Mission accomplished and now you know how to edit/change the PATH on your Mac OS X.

Step 6: You can confirm the new path by executing the following command:

echo $PATH

Now you will see the changes which you have made.

Mistakes one can make while following this process:

If you type ~/.bash_profile in your terminal window you would land on this following statement which says

-bash: /Users/<your username>/.bash_profile: Permission denied

It’s saying ‘permission denied’ because you are trying to run the .bash_profile as a command.When you type ~/.bash_profile you haven’t given it a command to execute against the file.

As the filename starts with the ‘.’ ,it is a hidden file.In terminal if you type ls -la it will give you a listing with all the files in a directory with a long format which provides more information about the files including the ownership privileges including all the hidden files.

So beware of making such a mistake! Have fun🙂

10 comments

  1. Normally I do not learn article on blogs, but I would like to say that this write-up very forced me to try and do so! Your writing taste has been surprised me. Thank you, very great post.

    1. By default this file does not exist on any version of OS X. And yes, it is up to you to create it manually:
      touch ~/ .bash_profile

      Alternatively, you could use nano as well if you want to edit the file at the same time:

      nano ~/ .bash_profile

      Note: Do not monkey in /etc unless you know what you’re doing.

      P.S: I am very sorry for the very very late response on your comment.

  2. Hi everybody! I’m trying with the two following command:

    touch ~/.bash_profile
    open ~/.bash_profile

    And then I become this from terminal “Trace/BPT trap: 5”

    What I suppose to do now?
    Thank’s in advance for your Help!

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s