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

Shell Bashing-Answer

© May 2001 Tony Lawrence

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.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> Shell Bashing

Inexpensive and informative Apple related e-books:

Take Control of Automating Your Mac

Take Control of iCloud, Fifth Edition

El Capitan: A Take Control Crash Course

Take Control of OS X Server

Take Control of the Mac Command Line with Terminal, Second Edition

More Articles by © Tony Lawrence

Printer Friendly Version

Have you tried Searching this site?

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

Printer Friendly Version

Getting information off the Internet is like taking a drink from a fire hydrant. (Mitchell Kapor)

Linux posts

Troubleshooting posts

This post tagged:



Unix/Linux Consultants

Skills Tests

Unix/Linux Book Reviews

My Unix/Linux Troubleshooting Book

This site runs on Linode