bash, /etc/profile, /etc/bashrc, and X11

Aug 27, '03 09:53:00AM

Contributed by: jrvela

First of all, I am new to Macs. I've got my first Mac about a week ago. I tried looking for a solution to my problems and I could not find it, or I did not look in the right place. Please excuse me if I am just repeating stuff that has been common knowledge. I am running MacOSX 10.2.6 on a PowerBook. Thus I can't tell you if my issues exist in other releases.

My shell of choice is bash. One of the first things I did to customize my Mac was to change the default shell for my account as well as the root account. Since bash is not the default shell, it seems like a few things did not work right at first.

Hint#1:
I wanted to add two alias to /etc/bashrc. I edited the file to look like this:

alias rm='rm -i'
alias mv='mv -i'
I did not want to do this in my .bashrc, becasue I wanted the alias to be used by all user accounts. It seems that for some reason /etc/bashrc does not get invoked when you start the Terminal, even though I do have /bin/bash as my shell. The shell starts, but the rc file does not run. Solution: Edit /etc/profile to have this line a the end:
.  /etc/bashrc
Yes, it starts with a dot and a space.

Since /etc/profile only gets executed with bash, I don't have to be concerned about running /etc/bashrc for other shells. If that were the case, then you would need to check $SHELL to handle that. This modification to /etc/profile and /etc/bashrc works pretty well when you start the Terminal application.

What these aliases do is invoke rm and mv commands with the -i option. Thus the system will prompt before deleting files unless you specify the -f option.

bash-2.05a$ rm test
remove test? 
The user must reply Y or N. If you don't want to be prompted then do rm -f test, and test will be gone.

Read the rest of the article for hints on keeping your scripts organized and information on changing the X11 startup routine.

Hint #2:
I like keeping my scripts orginized in their own directory. So I created a directory called bin under my home. Then I added these lines to /etc/profile:

PATH="$HOME/bin:$PATH"
export PATH
Now every user must have their scripts in the bin directory for them to be executable. Now when I do echo $PATH in the terminal application, I see that the bin subdirectory is in the PATH.

Hint # 3:
Hints 1 and 2 work great in the Terminal application, but when I start X11 (the one from Apple) and the xterm fires up automatically, my alias and PATH settings are gone. I started diffing through the X11 scripts and found out that the way Apple starts X11 it does two bad things:

  1. It ignores /etc/profile
  2. It sets its own PATH
Solution: Make sure that /etc/profile sets the PATH to contain /usr/X11R6/bin and that DISPLAY has been set properly. I think this has been discussed in other hints on this forum. If you don't have that in /etc/profile, just make sure you have this chunk of code there:
PATH="/usr/X11R6/bin":$PATH
export PATH
DISPLAY=:0.0
export DISPLAY
Edit /etc -> X11 -> xinit -> xinitrc as follows: Look for the line that invokes quartz-wm and add & at the end of the line to launch the quartz window manager in the background. After that line, insert some code to invoke /etc/profile if the shell is bash. Then move the xterm line from where it was originally to the end of the file. Also add the & to launch it in the background. So the modified xinitrc will look like this:
#!/bin/sh
# $Id: xinitrc,v 1.2 2003/02/27 19:03:30 jharper Exp $

userresources=$HOME/.Xresources
usermodmap=$HOME/.Xmodmap
sysresources=/etc/X11/xinit/.Xresources
sysmodmap=/etc/X11/xinit/.Xmodmap

# merge in defaults and keymaps

if [ -f $sysresources ]; then
    xrdb -merge $sysresources
fi

if [ -f $sysmodmap ]; then
    xmodmap $sysmodmap
fi

if [ -f $userresources ]; then
    xrdb -merge $userresources
fi

if [ -f $usermodmap ]; then
    xmodmap $usermodmap
fi

# start the window manager

exec quartz-wm &

# if bash shell execute /etc/profile

if [ "$SHELL" = "/bin/bash" ]
then
    . /etc/profile
fi

# start some nice programs
xterm &
After you save your changes, quit X11 and start it again. Then in the xterm window do alias, and you should get:
alias rm='rm -i'
alias mv='mv -i'
If you do echo $PATH, it should match the result that you get in the terminal application.

There is one thing I still have not figured out yet. If you launch another xterm from X11 -> Applications -> xterm, this will have its own wacky PATH and blow away your aliases. I figured that this is due to some bad assumptions made by quartz-wm in handling the environment. Since this is a binary and I don't have access to the source, I could not dig any further.

The workaround for this last issue is to do X11 -> Applications -> Customize, and remove the xterm application from the menu. Then when you want another xterm, just type the command xterm &. You can do this either in xterm or in the Terminal application, since your /etc/profile has the right settings to do so.

I assume that tcsh has similar issues and that the .login script needs to be modified and xinitrc should call it if the shell is tcsh. I did not take to time to look into this. On the other hand, maybe all my issues had to do with bash and all the tcsh scripts work fine. I hope some tcsh user can fill in the details.

Comments (13)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20030825001724805