Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

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

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.

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:

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:
export PATH
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:
# $Id: xinitrc,v 1.2 2003/02/27 19:03:30 jharper Exp $


# merge in defaults and keymaps

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

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

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

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

# start the window manager

exec quartz-wm &

# if bash shell execute /etc/profile

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

# 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.

  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)

bash, /etc/profile, /etc/bashrc, and X11 | 13 comments | Create New Account
Click here to return to the 'bash, /etc/profile, /etc/bashrc, and X11' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
bash, /etc/profile, /etc/bashrc, and X11
Authored by: stukoch on Aug 27, '03 11:25:18AM

Word is that bash will be default in 10.3


[ Reply to This | # ]
bash, /etc/profile, /etc/bashrc, and X11
Authored by: pcunixapl on Aug 27, '03 02:25:46PM
"Word is that bash will be default in 10.3"

Fantastic. It was SOOO dumb to have tcsh as the default..

See if you can't imagine why I'd say that.

Tony Lawrence

[ Reply to This | # ]

bash, /etc/profile, /etc/bashrc, and X11
Authored by: geofflane on Aug 27, '03 12:11:19PM

The reason that the different rc scripts are sourced based on whether or not you are in a login shell or not.
For information on what rc files get sourced by login vs. non-login shells see below.

From the bash man page:

When bash is invoked as an interactive login shell, or as a non-inter-
active shell with the --login option, it first reads and executes com-
mands 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. The --noprofile option may be used when the
shell is started to inhibit this behavior.

When a login shell exits, bash reads and executes commands from the
file ~/.bash_logout, if it exists.

When an interactive shell that is not a login shell is started, bash
reads and executes commands from ~/.bashrc, if that file exists. This
may be inhibited by using the --norc option. The --rcfile file option
will force bash to read and execute commands from file instead of

[ Reply to This | # ]
bash, /etc/profile, /etc/bashrc, and X11
Authored by: merlyn on Aug 27, '03 12:46:05PM
As I said in another thread, don't ever ever overload existing commands.

[ Reply to This | # ]
bash, /etc/profile, /etc/bashrc, and X11
Authored by: paulehoffman on Aug 27, '03 02:35:51PM

What merlin said! Don't change "rm", create "rmi" and stop using "rm". Really truly. You will get used to "rm" being "rm -i" on your daily machine, then you will go to a different machine, and you will invoke "rm" assuming that you will be prompted.

Then you will curse a lot.

[ Reply to This | # ]
bash, /etc/profile, /etc/bashrc, and X11
Authored by: truhe on Aug 27, '03 05:01:09PM

for bash:

use ". ~/.bashrc" in /etc/profile, to let every user have its own .bashrc

[ Reply to This | # ]
wacked xterm environments
Authored by: huzzam on Aug 27, '03 08:43:44PM

In your xinitrc, try putting the ". /etc/profile" BEFORE "exec quartz-wm &". Since quartz-wm launches all your other x programs (if you start them from the Applications menu), its environment gets passed on to its children, including your extra xterms.

[ Reply to This | # ]
wacked xterm environments
Authored by: jrvela on Aug 28, '03 07:45:56PM

Actallly that does not work. Because the issue is quartz-wm. It sets teh PATH to its own will. Thus you must call /etc/profile after "quartz-wm &". And you do have to launch it in the background (&) otherwise control will not return to xinitrc for the rest of the commands.

[ Reply to This | # ]
bash, /etc/profile, /etc/bashrc, and X11
Authored by: kerbaugh on Aug 28, '03 11:27:23PM

Aliasing rm to "rm -i" isn't a good way to implement the 2-step deletion process. Answering yes every time you want to remove a file is a waste of time and probably gets so tedious that careful checks aren't made. The Trash directory is an easier way to review deletions before final unlinking. I have a script that I call rmm that emulates rm in every way except that it moves the items to ~/.Trash instead of unlinking them. It also preserves resource forks so that the files will be usable in case the removal was a mistake. My script can be found at:

[ Reply to This | # ]
bash, /etc/profile, /etc/bashrc, and X11
Authored by: kholburn on Aug 28, '03 11:37:58PM

First of all the best way to add this to /etc/profile is
if [ -f /etc/bashrc ]; then
. /etc/bashrc

I have until very recently used tcsh because it has these 2 features that bash didn't used to have. History search and command completion. The latest version has these so here's how to get them:

Add these 2 lines to

control-p: history-search-backward
control-n: history-search-forward

If you are in a bash shell already you will need to run:
bind -f /etc/inputrc
otherwise /etc/inputrc should be loaded on login.

To use this: type the first few letters of a command then control-p and control-n while cycle through all the commands in your history that start with those letters. Very useful.

Also it is worth finding a good copy of the bash completions (bash_completion) put it at /etc/bash_completion

and add to /etc/bashrc

if [ -r /etc/bash_completion ]; then
. /etc/bash_completion

I'm not sure where to find a copy of bash_completion. It is probably linux oriented (some of the options maybe slightly different) but it's a great start.

Here's a copy of my /etc/bashrc:

if [ "$PS1" ]; then
PS1='\e[1m\u@\H:\w \n\$\e[0m '
if [ -f /etc/bash_completion ]; then
. /etc/bash_completion

export HISTSIZE=10000
export HISTCONTROL=ignoredups

alias ls='ls --color=auto -axF'
alias l='ls -al'

[ Reply to This | # ]
Authored by: gdelfino on Dec 19, '03 02:17:31PM
It didn't work for me under panther. X11 wouldn't launch. This is what console says:
XFree86 Version 4.3.0 / X Window System
(protocol Version 11, revision 0, vendor release 6600)
[DRI] screen 0 installation complete
Screen 0 added: 1344x1008 @ (0,0)
X connection to :0.0 broken (explicit kill or server shutdown).

X connection to :0.0 broken (explicit kill or server shutdown).

[ Reply to This | # ]
Authored by: ciani on Feb 25, '04 01:30:18PM

In xinitrc, remove the & after quartz-wm. I had the same problem and this fixed it. Here is how that segment looks on my computer:

# if bash shell execute /etc/profile

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

# start programs

xterm &

# start the window manager

exec quartz-wm

[ Reply to This | # ]
bash, /etc/profile, /etc/bashrc, and X11
Authored by: gshenaut on Feb 02, '04 09:47:57AM

To cause the standard login environment to be loaded when starting
an xterm from the X11->Applications menu, you can "customize"
the "Terminal" command such that instead of just "xterm" in the
"command" column it has "xterm -ls". You can also change the
default window size, etc. by adding the appropriate flags there.

[ Reply to This | # ]