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?

2
  • 1
    this question should be moved to apple.SE (if possible?), not closed.
    – Sparr
    Feb 12, 2014 at 21:20
  • @Sparr I totally agree with you but the moderators are only moving newer question not older ones. See discussion here
    – рüффп
    Apr 5, 2015 at 18:22

3 Answers 3

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
11
  • 8
    Could anyone explain what the [[ -s /file/path ]] is doing? Trying to Google for an explanation isn't too easy.
    – Shane
    Jan 6, 2013 at 2:23
  • 18
    From man bash: -s file True if file exists and has a size greater than zero.
    – ckruse
    Jan 6, 2013 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, 2013 at 8: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, 2013 at 8:47
  • 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, 2014 at 19:03
27

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

2
  • 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. Apr 29, 2014 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 5, 2016 at 5: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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.