Find the answer to your Linux question:
Results 1 to 10 of 10
What specifically is the difference between "Sourcing" a file and "Executing" a file? I've created a simple bash script called "scrpting" and put this in the /bin directory. I can ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Newbie
    Join Date
    Aug 2003
    Posts
    113

    ? Source ........ Execution ?


    What specifically is the difference between "Sourcing" a file and "Executing" a file?

    I've created a simple bash script called "scrpting" and put this in the /bin directory.

    I can type "scrpting" from any directory and the file executes.
    I can type "source scrpting" and the file executes. Or so I think it does...

    Is there an actual difference? Obviously this is very new to me...


    Thanks,
    JB

  2. #2
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    The difference is that when you execute it, the shell forks a new process in which it runs the script, while when you source the script, the shell actually opens the file and runs it itself, as if the commands were typed on the command line, in the same process.
    That way, if you source a script, all changes it makes to environment variables, the umask and everything will still be there when the script is done.

  3. #3
    Linux Newbie
    Join Date
    Aug 2003
    Posts
    113
    Thanks Dolda,

    You said:

    >That way, if you source a script, all changes it makes to environment variables, the umask and everything will still be there when the script is done.

    My response:

    >You mean if in the script, changes were made to environment variables and I sourced that script, those changes would take effect now or not..

    Or do you mean that no matter what environment variables I changed in the script, that after the script exited, my pre-exsisting environment variables (before I sourced the script) would be retained?

    So if I'm correct then, sourcing a script would be use to simple just test it out????

    Sorry...this is a little unclear to me.. because it seems that some scripts that are built into my distro (written by others) sometimes source other scripts.

    What are the various commands of sourcing a file/script...
    E..G.. "source file name" , ./filename or . ./filename....

    Just trying to tell the differenece of when a file/script is being executed or source from the command line or with in a script.

    Can you show me some examples of both so that I can see what the differences are.

    Thanks allot Dolda2000..
    JB

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    The thing is that when you source a file, it is exactly as if you typed the contents in it on the command line. Therefore, if a file contains "VAR=something" and you source it, it will be as if you ran "VAR=something", ie. the changes to the variable VAR stays afterwards. That is very useful for scripts to load settings from other files. For example, many of the files in /etc/sysconfig on RH are just scripts that are sourced by other scripts, in order to get the variable definitions in them.

    If you execute a script like you would with any other program, the shell makes no difference between the script and any other program, ie. it forks a new process which runs the script. Of course, any changes to environment variables that the script does still happen, but since it runs in another process, it doesn't affect the original shell.

    In scripts (or on the command line, naturally), you can create the same "isolation" effect for single commands by running them in a subshell. You do that by enclosing the command(s) in parentheses. For example, "(VAR=test)", will just fork a new shell that changes the variable VAR. It then exits, and the original shell continues, where the variable VAR never was changed. For example, try "(cd /usr; ls)". It will list the contents of /usr, while still leaving the working directory of the current shell where it was.

  6. #5
    Linux Newbie
    Join Date
    Aug 2003
    Posts
    113
    Thanks for the respnose Dolda.

    I will need to digest this a bit. My understanding is still a little clouded. But I'm confident that this will soon make sense. I will refer to your post and what other info I can find on sourcing to better understand this concept. Being new to linux, in genera,l is why my understanding of these concepts is going to take some time to fully sink in. I'm quite a visual type learner as well so sometimes it takes me longer to understand invisible concepts so to say.

    I'm so used to Windows where everything is done for you and you don't really have a whole lot of control. Never real worried about it till now.

    Linux is just the oppisite. You basically have control over just about everything it seems. Windows is like car that is an Auto-Transmission and linux is like a stick-shift and then some and I love this very much.

    I want to be able to tame the linux creature, I'm just not sure whats the best path, or where's the best place to start to achieve that objective. I want to have an deep understanding of my linux environment so that I can better harness that environment to suite my needs and or someone elses. And I don't want to leave any one stone unturned or misunderstood along the way.

    I was told that learning how to shell script will eventually give me a deep rooted understanding of how the linux environment works and how to use it to my advantage.

    Reading man pages help every now and then, but for a noob, such as myself, it can be like trying to understand an engineering schematic when I've never taken an engineering course.
    But I'm certain this is what everyone goes through and it eventually starts to make sense.

    Do you know of any mailing lists or forums that are just for bash or shell scripting? I can't really seem to find any. I've set a goal to start with the MAN BASH page and some shell scripting tutorials and I would like it if I could be able to have a place where I can ask questions from people that kindof specialize somewhat in that area. Like yourself. You seem to be very knowledgable in this area. Maybe I am better suited to pay for an online beginners class that alows me to pester an instructor with all of my questions. Heee...Heee.. Put my new Gaim IM client into overdrive..

    Sorry to be so long winded. Just a little frustrated and looking for some guidance..

    Thanks for all of your time Dolda2000. Very much appreciated
    JB

  7. #6
    Linux Engineer
    Join Date
    Apr 2003
    Location
    Sweden
    Posts
    796
    Its very easy to get in and learn ordinary shellprogramming, because you can start by executing normal commands and thing in a shell script and just execute it. And when you get more and more experienced you can add more advanced things like loop and tests. Like this

    Code:
    #!/bin/bash
    
    # A normal shell command...
    echo "Listing everythnig under /tmp"
    ls -l /tmp
    
    # A simple directory test
    echo "Checking if /tmp exists"
    
    if [ -d /tmp ]
       then
       echo "/tmp exists"
    else
       echo "/tmp doesnt exist"
    fi
    
    # A simple for loop
    echo "Looping throw stuff under /tmp"
    
    for i in `ls /tmp`
        do
       echo "Found $i"
    done
    Copy this text into a file and make it executeble with chmod 755 <filename> and then execute it with..

    ./<filename>

    You can also check..

    man bash
    man test

    and tldp that have great howtos..

    http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
    Regards

    Andutt

  8. #7
    Linux Newbie
    Join Date
    Aug 2003
    Posts
    113
    Thankyou very much Andutt and to everyone that has responded to my noobiance,

    This is exactly what I was wanting. Just a little guidance to help me learn how to fly (so to say) in
    in Linux/Unix.

    I'll be checking out your web site as well.
    JB

  9. #8
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    I'm pretty sure that you might want to read this HOWTO:
    http://www.tldp.org/HOWTO/Unix-and-I...WTO/index.html
    It contains a LOT of goodies in order to understand the operational environment and some more things.

    We have a subforum here, the "General Scripting" forum that is basically intended for shell scripting (and other scripting languages, but shell scripting has been by far the most popular topic there). Just ask away all you want there. After all, we are here because we want to answer questions, so don't be afraid to ask. Of course, when the question is a generic one we might point to external resources instead, but rest assured that it's not because we don't like to answer it ourselves; it's just unnecessary to write an entire essay on a subject when it has already been done before.

  10. #9
    Linux Engineer
    Join Date
    Apr 2003
    Location
    Sweden
    Posts
    796
    Glad i could help you, and as Dolda says dont hessitate to ask stuff...thats why we are here!!
    Regards

    Andutt

  11. #10
    Linux Newbie
    Join Date
    Aug 2003
    Posts
    113
    Sweet,

    Thanks Dolda and Andutt.

    I feel better now. I always feel like I'm bugging people with such noobian questions. So I appreciate the warm invitation to fire away with my questions of need be.

    I totally understand if I'm pointed to documentation that answers my questions. Been there, done that...


    Alrighty then.....onto the general programing forum I go...Wa..Hoo..


    Thanks again,
    JB

Posting Permissions

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