182

Whenever I open a new tab in Terminal using Cmd + T, it opens bash in the same directory, as the previous tab. This works fine when I'm in the ~ directory, but if I'm anywhere else, I get an error loading .bashrc

Last login: Sat Oct 15 21:10:00 on ttys002
-bash: .bashrc: No such file or directory
Jakub-Arnolds-MacBook-Pro:projects darth$ 

It looks like .bashrc is loaded via relative and not absolute path, because if I do source ~/.bashrc, everything works smoothly.

loaded bashrc

I think this is a OS X Lion related problem, because before the upgrade from Snow Leopard, I didn't have the same issue. But that might be caused by Terminal always opening at ~, I don't remember if it tried to open the same directory.

However the question remains the same, how can I make Terminal load ~/.bashrc via absolute path, and not relative?

Jakub Arnold
  • 85,596
  • 89
  • 230
  • 327
  • 1
    this question should be moved to apple.SE (if possible?), not closed. – Sparr Feb 12 '14 at 21:20
  • @Sparr I totally agree with you but the moderators are only moving newer question not older ones. See discussion [here](http://meta.stackoverflow.com/q/258349/628006) – рüффп Apr 05 '15 at 18:22

3 Answers3

364

Terminal opens a login shell. This means, ~/.bash_profile will get executed, ~/.bashrc not.

The solution on most systems is to "require" the ~/.bashrc in the ~/.bash_profile: just put this snippet in your ~/.bash_profile:

[[ -s ~/.bashrc ]] && source ~/.bashrc
Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
ckruse
  • 9,642
  • 1
  • 25
  • 25
  • 8
    Could anyone explain what the `[[ -s /file/path ]]` is doing? Trying to Google for an explanation isn't too easy. – Shane Jan 06 '13 at 02:23
  • 18
    From `man bash`: -s file True if file exists and has a size greater than zero. – ckruse Jan 06 '13 at 11:55
  • 1
    "Terminal opens a login shell" - what are the other types os shells ? Where can I find more documentation about it? – dknight Jul 30 '13 at 08:16
  • 4
    There are so-called „interactive shells” and „login shells.” Your bash manual (`man bash`) talks about it and explains it, chapter INVOCATION (just search for `INVOCATION` after calling `man bash` by typing `/INVOCATION`) – ckruse Jul 30 '13 at 08:47
  • It is possible to check whether the shell is interactive by issuing the following: `tty -s && echo "Interactive" || echo "Not interactive"`. This way, you can conditionally source files on whether the shell is interactive, which can be helpful in certain situations, as is the case for `scp`, which expects data transfer through stdin/stdout. – Nicolas De Jay Nov 29 '13 at 00:09
  • 3
    I fail to see how a question pertaining to a code execution situation, solved by a line of code, is "off topic" to a programming question site. – Kheldar Feb 15 '14 at 19:03
  • 1
    This is helpful but WRONG. Read the Bash man page - it is most likely loadgin /etc/profile – Steve Benner Apr 18 '14 at 23:28
  • 1
    @SteveBenner what is wrong? Bash manpage says: `When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.` Directly copied from the man page. So: `~/.bash_profile` IS loaded on a login shell. – ckruse Apr 29 '14 at 13:20
  • 1
    @ckruse I regret calling you out because its not a positive reaction, I apologize. My intention was to avoid confusing people, and your solution will not work uniequivocally; a better answer would be to explain the underlying concepts, such as taken from the man page as you have in your comment. Even more importantly, people should be aware that system configurations vary wildly, for instance your solution wouldn't work for me as I don't configure my shell in `~/.bash_profile`. – Steve Benner Apr 29 '14 at 22:49
  • 1
    @SteveBenner I'm not pissed. I've just been curious what you've been referring to. And the question was why `~/.bashrc` doesn't get loaded and how to get it loaded. That's what I explained :) Of course a longer answer with more details would've been better, but nobody is perfect :) – ckruse Apr 30 '14 at 06:17
  • @SteveBenner, I agree, in particular, if one had previously put configs in ~/.profile but not ~/.bash_profile then he should not use this solution, or it would not run ~/.profile once ~/.bash_profile is found and run. – Edy Jan 24 '18 at 02:49
27

Renaming .bashrc to .profile (or soft-linking the latter to the former) should also do the trick. See here.

scorpiodawg
  • 5,612
  • 3
  • 42
  • 62
  • Or you could symlink them: `cd && ln -s ./.bashrc ./.profile` Note that symlinks are sensitive regarding the file path, I believe that using the above syntax works, but to be sure you can use absolute paths in any case. – Steve Benner Apr 29 '14 at 22:42
  • 1
    Uh, no? These files have different purposes. If you have Bash syntax in `.profile` you will break regular `sh`. – tripleee Oct 05 '16 at 05:57
14

I have the following in my ~/.bash_profile:

if [ -f ~/.bashrc ]; then . ~/.bashrc; fi

If I had .bashrc instead of ~/.bashrc, I'd be seeing the same symptom you're seeing.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631