Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I came across this example when trying to mount a usb device inside a openvz container and I have never seen the construct in the second line before. Can you explain what it signifies?

#!/bin/bash
. /etc/vz/vz.conf
share|improve this question
    
Hi there! It appears you have a good answer below. Would you mind accepting it if it answers your question? It marks this question as resolved and rewards the author for their helpfulness. Thanks! –  Christopher Kyle Horton Oct 8 at 0:35

1 Answer 1

It's a synonym of the builtin source. It will execute commands from a file in the current shell, as read from help source or help ..

In your case, the file /etc/vz/vz.conf will be executed (very likely, it only contains variable assignments that will be used later in the script). It differs from just executing the file with, e.g., /etc/vz/vz.conf in many ways: the most obvious is that the file doesn't need to be executable; then you will think of running it with bash /etc/vz/vz.conf but this will only execute it in a child process, and the parent script will not see any modifications (e.g., of variables) the child makes.

Example:

$ # Create a file testfile that contains a variable assignment:
$ echo "a=hello" > testfile
$ # Check that the variable expands to nothing:
$ echo "$a"

$ # Good. Now execute the file testfile with bash
$ bash testfile
$ # Check that the variable a still expands to nothing:
$ echo "$a"

$ # Now _source_ the file testfile:
$ . testfile
$ # Now check the value of the variable a:
$ echo "$a"
hello
$

Hope this helps.

share|improve this answer
8  
Just a note: . will work in most shells (sh, ash, ksh, etc), source is specific for bash. –  EarlGray Dec 26 '12 at 22:31
    
@EarlGray source isn't just bash--it's in C-style shells (csh, tcsh)--and zsh too. . works in Bourne-style shells including those listed. Considering that bash is a Bourne-style shell and hardly any bash script of non-trivial complexity is likely to run in a C-style shell, it's true . should be considered much more portable. But bash's source synonym of . exists partly for portability. –  Eliah Kagan Oct 8 at 1:14
    
@EliahKagan yes, you're right; it's better to say that . is universally portable and source is wide-spread, but does not work in plain sh. –  EarlGray Oct 9 at 11:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.