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 was under the impression that source was a synonym for . in bash. However, it seems that in the .profile file, source doesn't work. This youtube video demonstrates that when source is used in ~/.profile to source a file foo, the variable defined in that file is not exported to subsequent shells. However, if instead the file is sourced using ., the variable is exported as expected.

Note that when I use source, the environment variable does NOT get exported, but when I use . it does.

share|improve this question
8  
Lightdm sources .profile in /bin/sh which is dash by default, and dash doesn't have a source command, it is a bash extension (see /usr/sbin/lightdm-session) –  Jason Conti Mar 19 at 17:57
    
They are the same. Post (pastebin) the contents of all your files, including the one you are sourcing. And what shell are you using ? bash or zsh? –  bodhi.zazen Mar 19 at 17:57
2  
Please include the explanation in your question so we don't need to go and watch some random youtube video to understand the issue. –  terdon Mar 19 at 17:57
    
That's it Jason! Please answer so I can accept. –  dmd Mar 19 at 18:02
1  
This is not a duplicate, please don't close. The OP knows they're supposed to be identical and has found a particular case where they appear not to be. –  terdon Mar 20 at 1:59
show 2 more comments

1 Answer 1

up vote 8 down vote accepted

They are exactly the same, as explained in man bash:

.  filename [arguments]
source filename [arguments]
    Read and execute commands from filename in the current shell
    environment and return the exit status of the last command executed
    from filename.  If filename does not contain a slash, file names in
    PATH are used to find the directory containing filename.  The file
    searched for in PATH need not be executable.  When bash is not in
    posix mode, the current directory is searched if no file is found in
    PATH.  If the sourcepath option to the shopt builtin command is turned
    off, the PATH is not searched.  If any argu‐ ments are supplied, they
    become the positional parameters when filename is executed.  Otherwise
    the positional parameters are unchanged.  The return status is the
    status of the last command exited within the script (0 if no commands
    are executed), and false if filename is not found or cannot be read.

The issue here is that source is a bash thing, the standard is actually .. Your .profile is only read by login shells and by some (not all) login managers. However, login managers (such as lightdm) will attempt to read (source) the file using the system's default shell, normally /bin/sh. On Debian-derived systems, /bin/sh is a symlink to /bin/dash and dash is a very simple, POSIX-compliant shell that is not bash and has no knowledge of the source keyword.

Therefore, the command is ignored, the file is not sourced and the variable is not defined. To illustrate:

$ cat foo
myvar='foo1'
$ source foo
$ echo $myvar
foo1

The same thing in dash:

$ echo $0
dash
$ source foo
dash: 11: source: not found
$ . ~/foo  ## dash needs the full path
$ echo $myvar
foo1
share|improve this answer
add comment

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.