[bash] How do I edit $PATH (.bash_profile) on OSX?

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

I am using Mac OS X 10.10.3

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?

Thanks!

This question is related to bash macos terminal path osx-yosemite

The answer is


Simplest answer is:

Step 1: Fire up Terminal.app

Step 2: Type nano .bash_profile – This command will open the .bash_profile document (or create it if it doesn’t already exist) in the easiest to use text editor in Terminal – Nano.

Step 3: Now you can make a simple change to the file. Paste these lines of code to change your Terminal prompt.

export PS1="___________________ | \w @ \h (\u) \n| => "

export PS2="| => "

Step 4: Now save your changes by typing ctrl +o Hit return to save. Then exit Nano by typing ctrl+x

Step 5: Now we need to *activate your changes. Type source .bash_profile and watch your prompt change.

That's it! Enjoy!


For beginners: To create your .bash_profile file in your home directory on MacOS, run:

nano ~/.bash_profile

Then you can paste in the following:

https://gist.github.com/mocon/0baf15e62163a07cb957888559d1b054

As you can see, it includes some example aliases and an environment variable at the bottom.

One you're done making your changes, follow the instructions at the bottom of the Nano editor window to WriteOut (Ctrl-O) and Exit (Ctrl-X). Then quit your Terminal and reopen it, and you will be able to use your newly defined aliases and environment variables.


If you are using MAC Catalina you need to update the .zshrc file instead of .bash_profile or .profile


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.


In Macbook, step by step:

  1. First of all open 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;

  1. Edit your bash profile: open -e .bash_profile

After you can save from top-left corner of screen: File > Save

@canerkaseler


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 below paths in 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

Determine which shell you're using by typing echo $SHELL in Terminal.

Then open/create correct rc file. For Bash it's $HOME/.bash_profile or $HOME/.bashrc. For Z shell it's $HOME/.zshrc.

Add this line to the file end:

export PATH="$PATH:/your/new/path"

To verify, refresh variables by restarting Terminal or typing source $HOME/.<rc file> and then do echo $PATH


For me my mac OS is Mojave. and I'm facing the same issue for three days and in the end, I just write the correct path in the .bash_profile file which is like this:

    export PATH=/Users/[YOURNAME]/development/flutter/bin:$PATH
  • note1: if u don't have .bash_profile create one and write the line above
  • note2: zip your downloaded flutter SDK in [home]/development if you copy and paste this path

Mac OS X doesn't store the path in .bash_profile, but .profile, since Mac OS X is a branch of *BSD family. You should be able to see the export blah blah blah in .profile once you do cat .profile on your terminal.


Examples related to bash

Comparing a variable with a string python not working when redirecting from bash script Zipping a file in bash fails How do I prevent Conda from activating the base environment by default? Get first line of a shell command's output Fixing a systemd service 203/EXEC failure (no such file or directory) /bin/sh: apt-get: not found VSCode Change Default Terminal Run bash command on jenkins pipeline How to check if the docker engine and a docker container are running? How to switch Python versions in Terminal?

Examples related to macos

Problems with installation of Google App Engine SDK for php in OS X dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac Could not install packages due to an EnvironmentError: [Errno 13] How do I install Java on Mac OSX allowing version switching? Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user) How can I install a previous version of Python 3 in macOS using homebrew? Could not install packages due to a "Environment error :[error 13]: permission denied : 'usr/local/bin/f2py'"

Examples related to terminal

Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave Flutter command not found VSCode Change Default Terminal How to switch Python versions in Terminal? How to open the terminal in Atom? Color theme for VS Code integrated terminal How to edit a text file in my terminal How to open google chrome from terminal? Switch between python 2.7 and python 3.5 on Mac OS X

Examples related to path

Get Path from another app (WhatsApp) How to serve up images in Angular2? How to create multiple output paths in Webpack config Setting the correct PATH for Eclipse How to change the Jupyter start-up folder Setting up enviromental variables in Windows 10 to use java and javac How do I edit $PATH (.bash_profile) on OSX? Can't find SDK folder inside Android studio path, and SDK manager not opening Get the directory from a file path in java (android) Graphviz's executables are not found (Python 3.4)

Examples related to osx-yosemite

How do I edit $PATH (.bash_profile) on OSX? pip install: Please check the permissions and owner of that directory Intel X86 emulator accelerator (HAXM installer) VT/NX not enabled MySQL does not start when upgrading OSX to Yosemite or El Capitan Setting environment variables via launchd.conf no longer works in OS X Yosemite/El Capitan/macOS Sierra/Mojave?