Find the answer to your Linux question:
Results 1 to 8 of 8
Hello I have made a script with some errors in it. When I execute it the codes in .bashrc are getting executed , the message that i get when i ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux User vickey_20's Avatar
    Join Date
    Mar 2009
    Location
    Mumbai, India
    Posts
    493

    Why is .bashrc getting read when executing a script


    Hello
    I have made a script with some errors in it. When I execute it the codes in .bashrc are getting executed , the message that i get when i first log in etc.....
    Only if I could understand the man pages
    Registered Linux user #492640
    OS: RHEL4,5 ,RH 9,Ubuntu

  2. #2
    Super Moderator devils casper's Avatar
    Join Date
    Jun 2006
    Location
    Chandigarh, India
    Posts
    24,729
    Post your script here.
    It is amazing what you can accomplish if you do not care who gets the credit.
    New Users: Read This First

  3. #3
    Linux User vickey_20's Avatar
    Join Date
    Mar 2009
    Location
    Mumbai, India
    Posts
    493

    Here are the contents

    Note: I executed this script on RHEL 4
    -----------------------------------------------------------------------------------------------------------------------------------
    File a
    -----------------------------------------------------------------------------------------------------------------------------------
    Code:
    . b
    echo $vadapav
    echo hello
    vashi
    echo Back to a shell
    -----------------------------------------------------------------------------------------------------------------------------------
    File b
    -----------------------------------------------------------------------------------------------------------------------------------
    Code:
    vadapav="newbombay"
    vashi()
    {
     echo   "Happy New Year"
    }
    -----------------------------------------------------------------------------------------------------------------------------------
    .bashrc
    -----------------------------------------------------------------------------------------------------------------------------------
    Code:
    # .bashrc
    # User specific aliases and functions
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    alias cls=clear
    alias lg="cls;ls -gho"
    # Source global definitions
    if [ -f /etc/bashrc ]; then
    	. /etc/bashrc
    fi
    n=/usr/notes/Essentials/
    setterm -foreground green -store
    sleep 1
    cls
    echo -e "\t\t\t\tWelcome Root"
    -----------------------------------------------------------------------------------------------------------------------------------
    How I executed the script
    Code:
    ./a
    Only if I could understand the man pages
    Registered Linux user #492640
    OS: RHEL4,5 ,RH 9,Ubuntu

  4. #4
    Just Joined!
    Join Date
    Dec 2006
    Posts
    85
    well not seeing the error i would imagine it has something to do with the fact that the variable "vadapav" is being declared in b, but then b is returning after the declaration, i dont think a variable declared in a shell stays declared after the shell exits. so when you call $vadapav the variable will be undefined...though an undefined variable will not cause an error...
    of course vashi was also defined in b and would cease being defined in a when b returns. and this WOULD probably cause an error.
    as for /etc/bashrc...i believe that gets executed whenever you run a shell, the statement #!/bin/bash is basically making this a sepperate shell within the shell, its running /bin/bash.
    both b and a should do this if im not mistaken...

  5. #5
    Linux User vickey_20's Avatar
    Join Date
    Mar 2009
    Location
    Mumbai, India
    Posts
    493
    But when the other shell scripts I have executed successfully they don't execute my .bashrc. Also '.' operator is used to make another script run in the same script making the variable available in the script that called it.
    Only if I could understand the man pages
    Registered Linux user #492640
    OS: RHEL4,5 ,RH 9,Ubuntu

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    11,173
    Whenever a bash shell is started (this includes starting a script that is not executed inline with ". script" or "source script"), it sources ~/.bashrc - this is how the shell is designed. Other shells act the same was - csh -> .cshrc for example.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  7. #7
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by the_ultimate_samurai View Post
    well not seeing the error i would imagine it has something to do with the fact that the variable "vadapav" is being declared in b, but then b is returning after the declaration, i dont think a variable declared in a shell stays declared after the shell exits. so when you call $vadapav the variable will be undefined...though an undefined variable will not cause an error...
    of course vashi was also defined in b and would cease being defined in a when b returns. and this WOULD probably cause an error.
    It's unrelated, but anyway, as the original posted said, the dot builtin actually dumps the contents of the second script in the shell where the first one is running. It's equivalent to the "source" command.

    as for /etc/bashrc...i believe that gets executed whenever you run a shell, the statement #!/bin/bash is basically making this a sepperate shell within the shell, its running /bin/bash.
    both b and a should do this if im not mistaken...

    Quote Originally Posted by Rubberman View Post
    Whenever a bash shell is started (this includes starting a script that is not executed inline with ". script" or "source script"), it sources ~/.bashrc - this is how the shell is designed. Other shells act the same was - csh -> .cshrc for example.
    Not exactly. The ~/.bashrc file should only be sourced when starting an interactive non-login shell. The key word is "interactive". The subshell that's spawned when you run a script is non-interactive, so ~/.bashrc shouldn't be sourced in this case. It isn't sourced on login shells either, in that case bash would look for ~/.bash_profile, ~/.bash_login and ~/.profile, in that order, and will source only the first one found, even if it's an empty file. The rest will be ignored.

    I have no idea why would it be sourcing that file on a non-interactive subshell.

  8. #8
    Just Joined!
    Join Date
    Dec 2006
    Posts
    85
    hmm...i see...though according to the bash man page, if you do not include a / before the source it will check you path variable, so unless b is in his path im not sure it would execute it...
    as for the bashrc...hmm...

    a few things that could help:
    the errors you get when it runs and either the contents of .bash_profile,.bash_login, or .profile whichever comes first.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •