Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

My .bash_profile won't update

I had a .bash_profile from before but when I edit it the new path isn't echoed. I even tried renaming the old .bash_profile and creating a new one but the old path is still there. What could be the problem? I also tried opening the .bash_profile with sudo but the path doesn't change. How can I get the path to change? 


I can get it to update in terminal by either typing out the export command or by typing "source .bash_profile". But why doesn't the .bash_profile run automatically?

Posted on Aug 14, 2019 1:54 PM

Reply
Question marked as Best reply

Posted on Aug 17, 2019 11:34 AM

Set your Zsh PATH environment variable as as you would in Bash. Here is my PATH that I set in both shells:


export PATH=".:$HOME/bin:/usr/local/bin:$PATH"



The first dot lets me run any executable located in the current directory by its name, and not preceded by the "./" character sequence to force the shell to know where I am. I keep shell scripts that I have written in my $HOME bin directory, so I can run these without being concerned about their PATH. Non-Apple executables get placed in /usr/local/bin by some package managers, so I need a path to these too. The trailing $PATH is the default entries that the System assigns to PATH.


Alot of the Googled examples of setting Zsh content also encourage people to install the "Oh My Zsh" enhancement package. I have not done so, because if I present Zsh solutions here, I do not want to make reference to some feature(s) that I may take for granted (and forget) requires the add-on package.

Similar questions

24 replies
Question marked as Best reply

Aug 17, 2019 11:34 AM in response to nancych14

Set your Zsh PATH environment variable as as you would in Bash. Here is my PATH that I set in both shells:


export PATH=".:$HOME/bin:/usr/local/bin:$PATH"



The first dot lets me run any executable located in the current directory by its name, and not preceded by the "./" character sequence to force the shell to know where I am. I keep shell scripts that I have written in my $HOME bin directory, so I can run these without being concerned about their PATH. Non-Apple executables get placed in /usr/local/bin by some package managers, so I need a path to these too. The trailing $PATH is the default entries that the System assigns to PATH.


Alot of the Googled examples of setting Zsh content also encourage people to install the "Oh My Zsh" enhancement package. I have not done so, because if I present Zsh solutions here, I do not want to make reference to some feature(s) that I may take for granted (and forget) requires the add-on package.

Aug 17, 2019 7:42 AM in response to nancych14

Here are the generic 'bash' rules for running shell initialization scripts. Other shells have different rules:

  • bash when run as a new login (a login shell is one whose first character of argument zero is a -, or one started with the --login or -l option), will look for one of the following files in the following order and run the first one it finds, and stop looking after that:
    1. $HOME/.bash_profile
    2. $HOME/.bash_login
    3. $HOME/.profile
  • bash run NOT as a new login (for example as a subshell) will look for $HOME/.bashrc and run it if it is found.

bash NEVER runs both. If a user wants both to be run, they must arrange for one to run the other.


The typical use of the .bash_profile (or .bash_login, or .profile) is to setup environment variables that are inherited by subprocesses and subshells, perform operations the user wants done during login, and establish any other state that will be inherited by subprocesses.


The typical use of the .bashrc is to set up shell aliases, the shell prompt ($PS1), and any other state that may be desired in a subshell.


Many users have .bash_profile (or .bash_login, or .profile) invoke .bashrc before they exit so that the first interactive login has all the same features as a subshell.


Of if you just want to have 1 shell initialization script to rule them all, create a symlink from your .bash_profile to your .bashrc

cd $HOME
ln -s .bash_profile .bashrc


I have seen some strange things with Terminal with respect to creating additional windows, where they do not always seem to be created as new logins. But I have not been able to find a rhyme or reason or a way to reliably reproduce this. My solution was to have my .bash_profile and .bashrc create environment variables that the other could use to decide if the other has not been run, and if not, to invoke it. But for most people the symlink idea above would work very well.

Aug 17, 2019 7:58 AM in response to nancych14

If Terminal is not running your .bash_profile when being launched and creating the first window, then I would ask if you are running bash as your shell. Because if you are running sh, zsh, ksh, csh or tcsh then a .bash_profile would not be appropriate.


I suspect a shell from the bourne family, as 'export' works, so that kind of rules out csh and tcsh. That leaves sh, ksh and zsh


The most reliable way to figure this out is to use Activity Monitor

Applications -> Utilities -> Activity Monitor

Activity Monitor -> View -> All Processes Hierarchically

Click on the "Process Name" column title so it is the sort order

Look for "Terminal"

Click on the expand triangles so you see the processes under Terminal. If you are using bash it should look like:

  • Terminal
    • login
      • bash

If you have a different shell, then bash should be some other name, such as sh, ksh or zsh


And I am going to have to learn more about zsh, because from Catalina forward any new Mac users will get zsh as their default shell, and I will not be able to assume bash and .bash_profile, and .bashrc are shell initialization scripts 😟

Aug 17, 2019 9:13 AM in response to nancych14

Wow Bob!! This is so detailed and helpful! Thank you. How do you learn all this?

Been using Unix systems since '85, and I live on the command line, even when using my Mac at work.


So....if I'm not running bash but I seem to be running zsh then how do I set my path in zsh?

Since I'm not a zsh user (yet 😀), so I did a "man zsh" and found that you can put your export command in:

  • $HOME/.zshenv -- I think this is always processed first
  • $HOME/.zprofile -- if a login shell (2nd in the order checked)
  • $HOME/.zshrc -- if this is an interactive shell (not zsh running a shell script) (3rd in the order checked)
  • $HOME/.zlogin -- if a login shell (last in the order checked)

I DO NOT know all the rules for what is invoked when and if some are not invoked (I do not have 30+ years with zsh, and as it is the first 20 years was with the bourne shell (sh), it has only been since 2005 that I've been a bash user, but by that time I had a lot of experience and knew what to look for).


(And by the way, what is zsh anyway and how can I learn more about it?) And the follow up question is do I need any of these bash files anymore?

man zsh

https://www.zsh.org/

Google "zsh" and you will find lots of articles

if you are familiar with bash, then try Googling "zsh vs bash"

If you do not know bash all that well, then just stick with Googling "zsh"


There are not a lot of zsh books published. I found

  • Learning Shell Scripting with zsh
  • From Bash to Z Shell: Conquering the Command Line

I have NOT read any of these, and I have never seen them in a Barnes & Noble, so I cannot comment on them.


NOTE: for 90+% of the zsh usage it will be the same as most other bourne shells (sh, ksh and bash). But the shell initialization files are the one area most users will run into the differences. Most of the other differences are going to be things that do not really affect the typical occasional Terminal user. It will ONLY affect people like me that lives on the command line at work, and has a large infrastructure of shell initialization files, shell scripts, etc...


Aug 17, 2019 9:35 AM in response to nancych14

I did not really answer 'what is zsh', although some of the suggested reading might tell you.


"In the Beginning..." all computer systems had some kind of manufactured dictated command processor, and you ran that and only that.


I do not know if Unix was the first, or just the most popular that while providing a default command processing utility (which they called the shell and named 'sh'), but Unix allowed the user to substitute their own or one written by someone else. And from this we got various other command processing shells. The ones provided by macOS are:

  • /bin/bash
  • /bin/csh
  • /bin/ksh
  • /bin/sh
  • /bin/tcsh
  • /bin/zsh


A Shell does many things, but most people see it as the program that generates the Terminal command line prompt, reads the commands typed, figures out what program (most commands are just programs) it is being asked to run (via PATH), then creates a subprocess running that program, and waits for the program to finish. When the program finishes the shell collects the program completion status (a value between 0 and 255) and makes that available in $? for testing by the user or a script.


That is the heart of what a shell does. Now it has many features, especially suited for writing scripts, and it has features to aid the interactive user, such as command history, command line editing, command completion, file name completion, command aliases, etc....


When Mac OS X 10.0 was released back in the early 2000's 'csh' was the default shell, which was OK by me, because I was a csh user at the time (although I wrote all my shell scripts using the bourne shell).


I think it was around Mac OS X 10.3 (Panther) that the default shell switched to 'bash' (Bourne Again Shell). I should take a moment to say that the first popular Unix shell was written by Steven Bourne and he set the base line for what a Unix shell would do and would kind-of look like.


For various GPLv3 licensing reasons (basically Richard Stalman not liking commercial vendor proprietary software that is not given away with the sources for free), Apple can not ship a new bash shell, so they either have to live with GPLv2 licensed bash 3.2.57 (rather old now), or switch to a shell that is kept up-to-date, but does not have a GPLv3 license. They chose zsh as the default shell for new user accounts in macOS Catalina 10.15.


I hope I've answered what a shell is

Aug 17, 2019 8:22 AM in response to BobHarris

Wow Bob!! This is so detailed and helpful! Thank you. How do you learn all this?


Ok, so I got into the activity monitor and found:



My mac is running:



So....if I'm not running bash but I seem to be running zsh then how do I set my path in zsh? (And by the way, what is zsh anyway and how can I learn more about it?) And the follow up question is do I need any of these bash files anymore?


But of course, the primary question is: how do I set my path in zsh?


Thank you again for your detailed reply!

Aug 17, 2019 2:47 PM in response to nancych14

When a new user account is setup on OS X and macOS prior to Catalina (10.15), the default login shell is automatically set to /bin/bash. Beginning with Catalina, that will become /bin/zsh. In my Terminal application preferences, my default profile's Shell tab has nothing set in Startup, but still defaults to Bash.


Unless you changed your account login shell in System Preferences : Users & Groups, or deliberately changed that Profile : Shell tab to run Zsh inside the Bash shell, the only other way to invoke the Zsh shell is in the Terminal command-line by typing the following with a return:


zsh



So I transition from Bash (purple) to Zsh (green) by configuring my .zshrc PS1 prompt (which is different syntax than Bash).



When I want to leave Zsh, I type control-D to quit the Zsh shell, and my Bash prompt returns.


Of course without anything set up in .zshrc, it may attempt to echo the now incompatible Bash prompt, or it may just stare at you from a blank screen as mine did until I configured some things in .zshrc (yes, it is the counterpart to .bashrc). Zsh is a shell on steroids and it can take a long time to learn hence the PDF documentation (more current that HTML) on the Zsh site. They haven't upgraded their site to https security so Safari will tell you it is an unsecure site.


In the Terminal, whether in Bash or Zsh, you can look at the macOS manual page for Zsh, which offers other Zsh man pages:


man zsh


As for Ruby, if you installed it via a package manager, and if the Ruby bits are in /usr/local/bin, then the PATH as I shared earlier will allow you to see them before it locates the System installed Ruby in /usr/bin/ruby.


I have been using Ruby Version Manager (RVM) to install newer Ruby versions into a dot-file folder (.rvm) in my Home folder, and RVM requires that configure some environment variables and alter my PATH to help it find those Ruby gem binaries. Your installation may be different if you are not using RVM.


Take advantage of the Reference Documentation for your version of Ruby in the ruby-lang dot org Documentation section.


Apple will be deprecating (but still including) Python, Ruby, and Perl in Catalina, but I hold out little hope that these scripting languages will be installed in macOS 10.16 in Fall 2020.

nancych14 wrote:

Wow!! I think that was it!!

I just found that I had to make the changes in the .zshrc file. Which I'm assuming is the zsh equivalent of the .bashrc file.

It seemed to work and the path was there when I opened another terminal session. I also noticed that there were lines already in that file that looked like they came about when I installed Rudy (haven't started studying it yet.)

So, just out of morbid curiosity, what would have caused my shell to go from bash to zsh? Could the installation of Rudy have done it or was it an upgrade of the Apple ios?

Thank you again! This will save me a lot of time while I'm trying to learn mongo!


Aug 15, 2019 3:31 AM in response to BobHarris

Hi, Thanks for the reply.


Yes, I did quit and re-launch.


The path I'm trying to export is:


export PATH:$PATH:/usr/local/mongodb/bin


I installed a local copy of mongodb but I need to set the path to use it. What's weird is that I have other paths such as to python and ruby that are still there even when I deleted and created a new .bash_profile. I've even restarted my mac thinking that the path would reset if the .bash_profile was gone but it didn't. I've looked in the .bashrc, /etc/path.d, /etc/paths, private/etc/paths and a couple of other places but there's nothing there. When I create a new path in the new .bash_profile it doesn't take even if I run it with sudo.


What could cause this and how can I fix it?

Aug 15, 2019 1:17 PM in response to nancych14

your .bash_profile is in your home directory?


Your .bash_profile is an ASCII text file

$ file $HOME/.bash_profile
/Users/raharris/.bash_profile: ASCII text


If you issue the following command, what happens?

source $HOME/.bash_profile


Do you get any errors?

If PATH was not set yet, is it set after the 'source' command?


If you do the following, are there any errors?

set -x
source $HOME/.bash_profile
set +x



Aug 16, 2019 9:46 AM in response to BobHarris

Hi!


Yes, it's in my home directory.


Yes, it's ASCII text.


When I run source $HOME/.bash_profile it runs and goes back to the prompt. If I do echo $PATH then the path is there, but as soon as I close the terminal and open another terminal window the path goes back to what it was before.


On the last (set -x ...) I didn't get any errors but when I exited the terminal and opened another terminal the path was gone again.


Very strange, isn't it?

Aug 16, 2019 9:58 PM in response to nancych14

I'm always confused what scripts are auto run when I start a terminal session, therefor I have put echo statements in all my profilish bash scripts.


Last login: Fri Aug 16 21:02:53 on ttys000
in /etc/profile         but claims to be in -bash
in /etc/bashrc     claims to be in -bash
in /Users/mac/.bash_profile   but claims to be -bash running under darwin14


in my .bash_profile

echo in ~/".bash_profile   but claims to be $0 running under ${OSTYPE}"


you should also add an echo statement to .bashrc


R

Aug 17, 2019 2:05 PM in response to VikingOSX

Wow!! I think that was it!!


I just found that I had to make the changes in the .zshrc file. Which I'm assuming is the zsh equivalent of the .bashrc file.


It seemed to work and the path was there when I opened another terminal session. I also noticed that there were lines already in that file that looked like they came about when I installed Rudy (haven't started studying it yet.)


So, just out of morbid curiosity, what would have caused my shell to go from bash to zsh? Could the installation of Rudy have done it or was it an upgrade of the Apple ios?


Thank you again! This will save me a lot of time while I'm trying to learn mongo!

Aug 17, 2019 2:18 PM in response to rccharles

Some of this is free m the days of multiuser systems.


In early Unix implementations, you did not have sudo so admins ran around logged in as ‘root’. It rather dangerous for a root user as they would be all over the place diagnosing issues in various user accounts, especially in the university environment.


As the root account is always privileged, tricking root into running your script or command in you home directory could get you things.


This was less of a problem for non-privileged accounts outside of a university environment.


In a single user per Mac running as NOT root, it is less of an issue.


And I’ve included . in my PATH for years. 😁

My .bash_profile won't update

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.