new tabs in Leopard Terminal not using .bashrc

scacinto

Registered
Greetings, I've been trying to find some hard and fast information about the implementation of the bash shell in Terminal of 10.5. Right now I have several complaints, namely that the new Terminal is lousy about using the .bashrc file. There are two places now (as opposed to one in the old Terminal) where it is possible to specify "startup commands". In the Preferences for Terminal, the first tab under 'Startup', the option "Shells open with:" is given. If I recall, this is what the old Terminal used as well. Here I specify

/bin/bash --rcfile /Users/scott/.bashrc

to read my .bashrc file. Only in Leopard, this does not actually seem to work (which I can tell as my 'ls' is not in the stupendous color I would expect from the alias ls='ls -G' in my .bashrc file.) This is all I needed to do before I upgraded Terminals to have my resource file read.

So I venture further into 'Settings', one tab over, and select the 'Shell' tab there. Here is another 'Startup' option, so I enter the same command here, selecting the 'Run command:' box. Still no go. After selecting the 'Run inside shell' box from the same tab on the third try, I get my settings. The only problem is that my terminal prompt is now a boring 'bash-3.2$' and when I create a new tab, the resource file is not used. Only if I create a new window will it again run the command in the shell and use the resource file.

Can anyone post with some information about how Terminal goes about selecting what files to use (/etc/private/bashrc, .profile, bash_profile, etc.) and how one might overcome what I've laid out above as annoyances?

Many thanks,

-S
 
What I do is I have my login shell set to bash. So, in the Startup tab, I have the default selected (/usr/bin/login) which uses bash as my login shell.

The files that bash reads at startup are listed in the man page. If you want it to use .bashrc for an interactive shell, then allow Terminal to start bash normally as a login shell.

The "Run command" option in the shell tab I'm guessing is to run a command when the terminal window opens. The "run in shell" option probably means to have the command run in your shell after it's started and after it's read /etc/profile, ~/.bash_profile and ~/.bashrc. It is lame that the Terminal help doesn't describe the option, as far as I can tell.
 
Using the default login works fine if you don't need an interactive shell and you have a .bash_profile file. This is actually what I did; just used the default settings in Terminal and copied my .bashrc file (now unused!) into a new .bash_profile file in my home directory. Now, all windows and tabs use my settings. I don't know why I didn't think to do this earlier, I've just never had a problem with bash before... but then, Terminal is not really bash is it?

cheers,

-S
 
bash should read any or all of /etc/profile, ~/.bash_login, ~/.bash_profile and ~/.bashrc in an interactive shell. Terminal shouldn't cause bash to behave any differently, i.e. if you you just have .bashrc, to get Terminal to use it, use the default login shell.

I just checked and renamed my .bash_profile to .bashrc and it reads .bashrc using the default shell in the Startup tab in Terminal.
 
I misread the man page, and confused myself. It doesn't read .bashrc if you use your login shell.

A login shell reads the first of /etc/profile, ~/.bash_profile or ~/.bash_login. An interactive shell reads ~/.bashrc or the file you override with --rcfile. A non-interactive shell reads none of these.

If you use the default in Terminal, it starts a login shell. If you open a new tab it does the same. This is normal and desirable behavior I think. I have had a .bash_profile and no .bashrc. I would have .bashrc if my evil system administrator was forcing me to use csh for my login shell, and I had a habit of typing "bash" to startup my bash shell.

I think I see what you might be talking about with the different behavior in tabs though. If you add a command to the "Shell" tab in "Settings" in preferences, it doesn't run when you open new tabs, but that's not where you specify your shell... The option to run the command in your shell ("Run in shell") should be a giveaway that this isn't where to specify your unix shell (why would you run your shell in your shell?).

I gather, the Startup tab is for selecting the unix shell that Terminal will launch. The other tab that is confusingly called "Shell", allows you to specify a command to run within your unix shell or wihout your unix shell, e.g. telnet. I suppose any comand you specify here could be considered a shell, .i.e. telnet is a shell for entering telnet commands into, so the name isn't entirely weird.

As a demonstration, I created test.sh:

#!/bin/bash
echo $0
cat > /dev/null

Now with my startup shell selected in Startup (the default, using my login shell). If I have no command entered in the Shell tab, launching a new window and/or a new tab starts my shell as a login shell, .i.e it reads my ~/.bash_profile. Opening a new tab does the same.

Next if I select my test.sh script in "Run command" in the "Shell" tab in "Settings" in preferences, and leave "Run in shell" unchecked, if I open a new window, or launch terminal, it run's my script. When I press Ctrl-D the window closes (because I have "Close if the shell exited cleanly" selected), or I guess it would say "Shell exited" or some such thing. While the window is open, if I open a new tab it opens a unix shell window, and doesn't run my script again in the new tab.

Next if I check "Run in shell" it runs my script in my unix shell, if I open a new window (or launch terminal). If I press Ctrl-D it returns me to my unix shell.

So, I guess use .bash_profile instead of .bashrc, use the default in Startup in preferences and remove any command you have entered in the Shell tab in Settings in Preferences.
 
Several points.

Most importantly:
  • To fix this in windows and tabs in terminal.app: put commands in .bash_profile.

Also note:
  • .bashrc is used by non-interactive shells.
  • .bash_profile is used by interactive shells.
  • A common idiom is to set up .bash_profile to call .bashrc.


-Colin
 
Back
Top