This is the answer to Shell Bashing
As we know, Kevin wanted his script to run when he logs in. If he had been running /bin/sh instead of bash, he would have called his script from .profile, but bash gives you more choices.
Bash prefers to find its startup in either .bash_profile or .bash_login. These two are interchangeable- bash will use either one- if it finds .bash_profile, it will use that, if it doesn't, it will look for .bash_login. If it doesn't find either, it will then look for .profile. So Kevin could have put his commands in any of these.
Instead, he put the line that called his script in .bashrc and that's what caused all his problems. Bash will run .bashrc when you login, but it also runs it whenever a new bash shell is launched- which is just what happens when you run a script.
Try this on a Red Hat system (you won't be able to duplicate this on a SCO system or even a BSD system- it seems to be specific to Linux):
Create a script in your home directory called "t".
:
echo running
Now edit your .bashrc to add these lines:
echo Calling t
$HOME/t
echo called t
Now run "./t". You'll get a string of
"Calling t"
until you interrupt with CTRL-C. Change the "t" script to:
#!/bin/sh
echo running
and there will be no loop. That's because even though /bin/sh is a link to /bin/bash, bash acts differently when it is called as sh: if it's interactive it will ONLY look for .profile, and, interactive or not, it will never run .bashrc. The solution, of course, is not to put "#!/bin/sh" at the top of the script- the line that calls it should be taken out of .bashrc and put in .bash_profile. The .bashrc file should be used for variables and aliases that you want set to a known value when you run a script or start a new shell.
Publish your articles, comments, book reviews or opinions here!
© May 2001 A.P. Lawrence. All rights reservedEnter your email address for automatic notification of new posts here
(be sure to whitelist 'feedburner.com' if you use spam filtering)
| Views for this page | ||||
|---|---|---|---|---|
| Today | This Week | This Month | This Year | Overall |
| 4 | 6 | 6 | 554 | 2,864 |
Have you tried Searching this site?
Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.
------------------------
Comments are closed for this page. You can comment at the "Answer" page.
comment on this page here
Installation and light training Boston and New England
Reliable and experienced, punctual and professional.