.profile and .bashrc doesn`t work on my Mac

I had a similar problem with my .profile not being run. It turns out (as explained in this Apple StackExchange answer) that if you have .bash_profile or .bash_login files, then your .profile will be ignored by bash.


I guess this Q&A should go to Superuser, but anyway:

According to the section INVOCATION of the man page,

  • /etc/profile and ~/.profile is read for an interactive login shell, and
  • ~/.bashrc is read for an interactive non-login shell.

If your other UNIX machine automatically read ~/.bashrc even for an interactive login shell, that's because the system-wide /etc/ profile has a line which reads ~/.bashrc. OS X's system-wide /etc/profile doesn't have one.

So, if you want to run ~/.bashrc even for an interactive login shell, you need to include a line

. ~/.bashrc

in your ~/.profile.


Try sourcing them "by hand":

source ~/.profile

and see what happens. Note that, as others have pointed out, .profile is sourced by login shells, .bashrc by non-login shells; so try one or the other, not both.


In your terminal preferences, check in the 'Startup' pane. You have options:

Shells open with:

  • Default login shell (/usr/bin/login)
  • Command (complete path)

I have 'default' chosen...if you have the custom command chose, that may be a factor.

The next point to check is whether the shell is started as a login shell. When I do a ps listing, I see:

  PID TTY           TIME CMD
24088 ttys000    0:00.03 -sh
24614 ttys001    0:00.03 -sh
25127 ttys002    0:00.05 -sh
35544 ttys003    0:00.08 -sh
40926 ttys004    0:00.03 -sh

The key point here is the '-' in front of the shell name; that tells the shell to run through the profile and related stuff. If you don't see that in your current windows, you may need to diddle with the settings until you do.

The settings pane in the terminal preferences also lists a shell option. I have /bin/sh listed in mine.


You should not need this

If the worst comes to the worst, you can do as I did on some other uncooperative systems in times past - I created a program 'loginsh' which I could run from windows, and it in turn would execute my chosen shell with the '-' prefix to tell it to work as a login shell.

#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "stderr.h"
#include "jlss.h"

#ifndef DEFAULT_SHELL
#define DEFAULT_SHELL   "/bin/sh"
#endif /* DEFAULT_SHELL */

#ifndef MAX_SHELLBASENAME
#define MAX_SHELLBASENAME   256
#endif /* MAX_SHELLBASENAME */

#ifndef lint
static const char sccs[] = "@(#)$Id: loginsh.c,v 4.2 2005/06/22 19:44:07 jleffler Exp $";
#endif

int main(int argc, char **argv)
{
    char     *shell;
    char      shellname[MAX_SHELLBASENAME];

    err_setarg0(argv[0]);

    /* Which shell to use? */
    if ((shell = getenv("SHELL")) == (char *)0)
        shell = DEFAULT_SHELL;

    /* Set up argv[0] in new argument list; reuse old argv space */
    shellname[0] = '-';
    strcpy(&shellname[1], jlss_basename(shell));
    argv[0] = shellname;

    /* Execv must work -- the shell must be an executable program */
    execv(shell, &argv[0]);
    err_syserr("cannot execute shell %s\n", shell);

    /* NOTREACHED */
    return(EXIT_FAILURE);
}

(The "stderr.h" header and 'err_*' routines are an error reporting package I use everywhere. The jlss_basename() function is basically the same as system provided versions such as are found in POSIX <libgen.h>.)


Good information above, but thought I'd share some specifics on the problem I had and the solution I found.

The need: Add a directory to PATH on OSX 10.8 (ML).

The problem: Guidance is often "mod the .profile in your home directory". A bit confusing since, by default, this file doesn't exist on OSX. Doubly confusing because once you add an export statement to append the new directory to the existing PATH ... it doesn't work. Covered above.

The solution: I added the export statement to my .bash_profile (also in the home directory).

I'm sure there are many ways to skin this cat. This one worked for me. Somewhat confidence inspiring: the .bash_profile already existed and I could see that other installed software (e.g. Ruby) had modified it. If there's a better solution I'll be glad to hear about it!