APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Shell Bashing-Answer

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
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:

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.

This seems to have been fixed with more recent bash. See Don't ever change, baby

Got something to add? Send me email.

Increase ad revenue 50-250% with Ezoic

More Articles by

Find me on Google+

© Tony Lawrence

Kerio Samepage

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.

Contact us