I don't understand 'source' for sourcing a file.

Discussion in 'all things UNIX' started by Ocky, Jul 29, 2009.

Thread Status:
Not open for further replies.
  1. Ocky

    Ocky Registered Member

    Joined:
    May 6, 2006
    Posts:
    2,713
    Location:
    George, S.Africa
    Came across this in some instructions for installing java jre in CentOS.
    This part:-
    gedit /etc/profile.d/java.sh

    export J2RE_HOME=/opt/jre1.6.0_14
    export PATH=$J2RE_HOME/bin:$PATH

    source /etc/profile.d/java.sh

    Whether or not I 'source' the file, java works perfectly in all browsers.
    Can someone kindly explain when and why one should 'source' a file ?
     
  2. Mrkvonic

    Mrkvonic Linux Systems Expert

    Joined:
    May 9, 2005
    Posts:
    10,219
    Instead of running 20 export commands every time you need something, you can place them in one file and then source that file. Source means parsing and executing commands in it, in the current shell. This means that the file may work in bash but not in tcsh - or vice versa. Good example is indeed export in bash versus setenv in tcsh. You don't set the syntax in the file. You use it as is, hence the phrase source.

    So rather than executing export commands (or alias or anything else) 20-30 or 100 times one after another, place them in a file and run them all together, sort of a script, except you literally parse the contents.

    Good enough?

    Mrk
     
  3. Ocky

    Ocky Registered Member

    Joined:
    May 6, 2006
    Posts:
    2,713
    Location:
    George, S.Africa
    Well, I get the gist of it in general terms (emphasis on 'general').
    For instance I could source a file in .bashrc that would help me
    completing a certain type of task the fast and easy Mrk way. :D
    As for the tcsh shell - I reckon that's for the more advanced users.
    I am just happy that everything is working for me.
    Thanks for explanation and regards.
     
  4. Mrkvonic

    Mrkvonic Linux Systems Expert

    Joined:
    May 9, 2005
    Posts:
    10,219
    bash and tcsh are roughly equivalent. No emphasis on "advanced" users.
    You either use or both, nothing major ... Some syntax ...
    Mrk
     
  5. lotuseclat79

    lotuseclat79 Registered Member

    Joined:
    Jun 16, 2005
    Posts:
    5,390
    Hi Ocky,

    Source <file> produce the same effect as from your user account using '.':
    $ . <file>
    similar to the command to acquire the latest revisions to your .bashrc file:
    $ . .bashrc

    In either case, the source command instructs the command interpreter defined by SHELL to evaluate the <file> as a script.

    The above command executes the latest revision of the file .bashrc in your user home directory accout, and any files it invokes such as .bash_aliases. So, for example, you can add the alias clr='clear' to the file .bash_aliases, then execute . .bashrc, and then the command alias to print out all your aliases - and you should see the new alias clr.

    -- Tom
     
Thread Status:
Not open for further replies.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.