How do I edit $PATH (.bash_profile) on OS X?

904,461

Solution 1

You have to open that file with a text editor and then save it.

touch ~/.bash_profile; open ~/.bash_profile

It will open the file with TextEdit, paste your things and then save it. If you open it again you'll find your edits.

You can use other editors:

nano ~/.bash_profile
mate ~/.bash_profile
vim ~/.bash_profile

But if you don't know how to use them, it's easier to use the open approach.


Alternatively, you can rely on pbpaste. Copy

export ANDROID_HOME=/<installation location>/android-sdk-macosx
export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools

in the system clipboard and then in a shell run

pbpaste > ~/.bash_profile

Or alternatively you can also use cat

cat > ~/.bash_profile

(now cat waits for input: paste the two export definitions and then hit Ctrl + D).

Solution 2

A bit more detailed for beginners:


Before you begin with .bash_profile on Mac, please be aware that since macOS Catalina zsh (z shell) is the default shell. Therefore stuff we used to put in the .bash_profile now belongs to the .zshenv or the .zshrc file.

.zshenv .zshrc ? (Found here)

.zshenv: invocations of the shell. Often contains exported variables that should be available to other programs. For example, $PATH.

.zshrc: Sourced in interactive shells only. It should contain commands to set up aliases, functions, options, key bindings, etc.


STEP 1

Make sure the .bash_profile file is existing? (or the .zshenv of course) Remember that the .bash_profile file isn't there by default. You have to create it on your own.

Go into your user folder in finder. The .bash_profile file should be findable there. -> HD/Users/[USERNAME]

Remember: Files with a point at the beginning '.' are hidden by default.

To show hidden files in Mac OS Finder:

Press: Command + Shift + .

If it's not existing, you have to create .bash_profile on your own.

Open terminal app and switch into user folder with simple command:

cd

If it's not existing, use this command to create the file:

touch .bash_profile

STEP 2

If you can't memorise the nerdy commands for save and close in vim, nano etc (the way recommended above) the easiest way to edit is to open .bash_profile (or the .zshenv) file in your favored code editor (Sublime, Visual Studio Code, etc.).

Finder -> User folder. Right click -> open with : Visual Studio Code (or other code editor). Or drag it on app in dock.

… and there you can edit it, pass export commands in new lines.

Solution 3

If you are using macOS v10.15 (Catalina), you need to update the .zshrc file instead of file .bash_profile or .profile.

Solution 4

For Mac OS, step by step:

  1. First of all, open a terminal and write it: cd ~/

  2. Create your Bash file: touch .bash_profile

    You created your ".bash_profile" file, but if you would like to edit it, you should write it;

  3. Edit your Bash profile: open -e .bash_profile

    After that you can save from the top-left corner of screen: FileSave

Solution 5

Set the path JAVA_HOME and ANDROID_HOME. You have to open terminal and enter the below cmd.

touch ~/.bash_profile; open ~/.bash_profile

After that, paste the below paths in the base profile file and save it:

export ANDROID_HOME=/Users/<username>/Library/Android/sdk 
export PATH="$JAVA_HOME/bin:$ANDROID_HOME/platform-tools:$ANDROID_HOME/emulator:$PATH"
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home
Share:
904,461

Related videos on Youtube

Thiago
Author by

Thiago

Updated on July 28, 2022

Comments

  • Thiago
    Thiago almost 2 years

    I am trying to edit an entry to PATH, as I did something wrong.

    I am using Mac OS X v10.10.3 (Yosemite)

    I have tried:

    touch ~/.bash_profile; open ~/.bash_profile
    

    But the file editor opens with nothing inside.

    My problem:

    I am trying to install ANDROID_HOME to my PATH

    I misspelled it, but when I closed the terminal and went back it was gone, so I tried again:

    export ANDROID_HOME=/<installation location>/android-sdk-macosx
    export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
    

    This time, I typed the command correctly but, when I closed the terminal, my settings disappeared again.

    How do I execute my desired settings?

    If I was to edit bash.profile, how would I enter the above code?

    • Droppy
      Droppy almost 9 years
      Use vi ~/.bash_profile or subl ~/.bash_profile or mate ~/.bash_profile, depending on your favourite editor.
    • Thiago
      Thiago almost 9 years
      just tried nano ~/.bash_profile, it loads, but there is nothing inside
    • Droppy
      Droppy almost 9 years
      Sounds like .bash_profile is empty/missing then.
    • mfaani
      mfaani over 5 years
      For newbies like, these notes will help: 1. ~/.bash_profile means it's located in the root directory. ~ means root directory. 2. files prefixed with . are invisible to ls command. They are kind of like hidden files, files the normal user doesn't really need to see. Our case here is an exception. To be able to see it you can do ls -a 3. touch will create a file at the specified directory if it doesn't exist. It it does exist then nothing will happen
    • mfaani
      mfaani over 5 years
      4. and obviously open will open it with your default texteditor. 5. As a result doing touch ~/.bash_profile from any directory will work. because your path is not relative
    • Davis Herring
      Davis Herring about 3 years
      @Honey: ~ means home (i.e., $HOME), not root (i.e., /).
  • Droppy
    Droppy almost 9 years
    There are issues using things like cat to write to files when the text contains references to environment variables as it means those references need to be escaped. It's must more usual to use a text editor.
  • Mallory-Erik
    Mallory-Erik almost 8 years
    I've personally found accessing "hidden" files normally to be a plus with emacs...
  • tripleee
    tripleee over 7 years
    As of Yosemite (10.10) this is no longer true, if it ever was. Bash has been the default shell on macOS for a long time already. Historically, *BSD had csh which is incompatible, and uses .cshrc and not .profile (though if you need to use several Bourne-compatible shells, it makes sense to have common settings in .profile).
  • Herr_Hansen
    Herr_Hansen over 5 years
    I updated the way, to show hidden files in Mac OS's Finder. Press: Command + Shift + .
  • Dronz
    Dronz over 5 years
    What would it mean if I enter "touch ./bash_profile" (it says nothing) then "open .bash_profile" and it tells me that file does not exist?
  • Dronz
    Dronz over 5 years
    For some reason, touch did not create the file, but I used cat to do it as in Alessandro's answer.
  • ricks
    ricks over 4 years
    Wow, i've been going crazy trying to figure out why i can't update bundler and why my system is looking for the wrong path for ruby. Should i copy paste my bash_profile to zshrc?
  • EdwinCab
    EdwinCab almost 4 years
    Im using catalina, but I dont find that file, can you explain what is the directory ?
  • Paolo
    Paolo almost 4 years
    This. Also make sure to close the terminal and open a new one to see flutter commands work
  • loretoparisi
    loretoparisi almost 4 years
    WTF, this is crazyness!
  • kevin_theinfinityfund
    kevin_theinfinityfund almost 4 years
    You need to make sure your terminal is now zsh with chsh -s /bin/zsh. That will activate your terminal to automatically be zsh. Then you can make changes in ~/.zshrc or ~/.zprofile.
  • Richard Miller
    Richard Miller almost 4 years
    THANK YOU. If you don't want to type everything again, you can simply do: mv .bash_profile .zshrc
  • sss
    sss over 3 years
    if you are using Catalina, follow the steps outlined below : mkyong.com/maven/install-maven-on-mac-osx/…
  • Peter Mortensen
    Peter Mortensen about 3 years
    Re "base profile file": Do you mean "the Bash .profile file"?
  • Dror Bar
    Dror Bar over 2 years
    Replace bash_profile with zshrc from this answer
  • Dror Bar
    Dror Bar over 2 years
    In other words, what you want to do now is touch ~/.zshrc; open ~/.zshrc