Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want my test script could be run without initial startup scripts.

I found the option "--norc" can not work. The bash version is 4.2.0(2)

my ~/.bashrc contains

echo "in .bashrc"

I want to bash --norc -x test.sh output a content without .bashrc.

$ touch test.sh
$ bash -x test.sh
+ echo 'in .bashrc'
in .bashrc

$ bash --norc -x test.sh
+ echo 'in .bashrc'
in .bashrc

I found this problem only occurs on my Embedded system. The real Linux does not have such problem.

share|improve this question
1  
Does "echo $BASH_ENV" produce any output? The value of that parameter is used as the name of a file to source even for non-interactive shells; perhaps it is set to ".bashrc" somewhere. –  chepner Feb 15 '13 at 18:54
add comment

2 Answers 2

up vote 1 down vote accepted

As I mentioned in my comment, non-interactive shells aren’t supposed to read .bashrc at all.  So your question shouldn’t be “Why is --norc not working?”; your question should be “Why is this shell script (non-interactive shell) reading .bashrc in the first place?”.  One possibility is that you have a BASH_ENV environment variable that is set to ~/.bashrc.  If that’s not it, I don’t know, but here are some things you might want to try:

  • If your script isn’t doing anything bash-specific, try running it with sh; i.e., sh -x test.sh.
  • Create a testrc file with an echo in it, and say bash -–rcfile testrc -x test.sh.
  • Say bash -–rcfile /dev/null -x test.sh.
  • Any and all of the above without -x.  (This shouldn’t make a difference, but this problem already doesn’t make any sense.)
  • Run the script as a normal program: chmod +x test.sh and then just test.sh (or ./test.sh, if you don’t have . in your search path).  (You will need to have #!/bin/bash, #!/bin/sh, #!/usr/bin/env bash, or something equivalent as the first line of test.sh, but that’s a good practice anyway –– see grawity’s discussion.)  (But this shouldn’t make a difference, either.)
  • And really, you should do this first, but I didn’t want to annoy you and make you ignore the rest of my answer: Make sure the problem is what you say it is.  Check, double-check, and triple-check test.sh for any source or . commands that might (maybe indirectly) pull in .bashrc.  Similarly, see whether it calls any other scripts that might be doing it.  Verify that the in .bashrc message is really coming from .bashrc –– try renaming .bashrc (e.g., to .bashrc.hold, or whatever) and/or chmoding it to 0, and try again.
share|improve this answer
    
I've simplified my question, so I just use touch to create empty shell script. I've tested all your suggestion before asking. This is very strange. I'm testing it in embedded system. But when I test on Linux box, it seems work. –  Daniel YC Lin Feb 17 '13 at 1:20
    
The major reason is 'BASH_ENV', I unset it, then it works. –  Daniel YC Lin Feb 17 '13 at 2:06
add comment

--norc only applies to interactive shells; you are starting a non-interactive shell by providing the test.sh argument. Try adding the -i flag as well to force an interactive shell. (Note: this may leave you in the new shell after test.sh completes; I haven't tested.)

share|improve this answer
    
That doesn’t make much sense. The reason --norc applies only to interactive shells is that non-interactive shells aren’t supposed to read .bashrc at all. Sure, I’m interested in hearing whether specifying -i fixes the problem, but it seems like a peculiar way to try. –  Scott Feb 15 '13 at 18:48
    
Good point; I missed the forest for the trees. I guess the real question is, why is he seeing output from .bashrc even without --norc. –  chepner Feb 15 '13 at 18:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.