User login

Including a file in a bash script: that's what dot file does

To source a file in a shell script is similar to the way one can include or require a file in PHP.

http://humanreadable.nfshost.com/sdeg/bash_startup.htm

Some users might be unfamiliar with the term sourcing. This term means that a script uses another file as though that file was written directly into the parent script. The convention used to source a file is the command source or using a dot convention. Either of the following conventions is acceptable:

. /etc/bashrc

source /etc/bashrc

In this example both lines will source the /etc/bashrc file into a script. Many people use the dot convention, but remember that for new users the dot convention probably means nothing and if using a text editor that displays tabs as dots, could lead to additional confusion. Using the source command is more friendly for new users. One caveat to remember about sourcing other files is that the file must be sourced before the information in that file is needed. Usually, therefore, all files that are sourced in a script are sourced at the beginning of the script.

Resolution

Searched words: 
dot include file period decimal point full stop

Comments

Its not the same

starting a script within another by
$./script.sh
means that a new process is created for
running the script.

invoking the script by the source command
is like the lines of script.sh would have
been written at this place.

Try a script.sh with the contents

u='Hello'

$chmod +x script.sh
$./script.sh
$echo $u

$

and

$source script.sh
$echo $u
Hello
$

Greetings

@Its not the same

The author talks about sourcing, and about the "dot" convention.

Your newbie example is NOT using the "dot" convention. You are completly mistaking with your comment.

To sum up what the author explained and your comment, it is THE SAME to do either:

$ source script.sh

or

$ . ./script.sh

Notice the difference, the first dot IS the "dot" convention (way to launch/source/include a script), the second dot is part of the file path to access script.sh

Regards.

Source and dot are the same if the dot is followed by a space

Benjamin is correct that dot space filename will include the file the same as "source" will. The space between the dot and the filename, while it is present in Benjamin's post, isn't really obvious. Perhaps using a courier font would make it more noticable:

$ cat > script.bash       # Create the script
u='Hello'
                          # Typed a control d here to
                          #terminate the script
$ chmod 700 script.bash   # Make the script exectable
$ echo $u                 # Show that u isn't set.

$ . script.bash           # Execute the script
$ echo $u                 # Show that u is now set.
Hello
$

El Coquero is correct that dot/filename will execute the script "filename" from the current directory.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.