0

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.

1
  • 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, 2013 at 18:54

2 Answers 2

1

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.
2
  • 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. Feb 17, 2013 at 1:20
  • The major reason is 'BASH_ENV', I unset it, then it works. Feb 17, 2013 at 2:06
1

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

2
  • 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. Feb 15, 2013 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, 2013 at 18:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .