Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using dot or "source" while calling another script - what is the difference?

Let's take a little example:

$ cat source.sh #!/bin/bash echo "I'm file source-1"  . source-2.sh 

And:

$ cat source-2.sh #!/bin/bash echo "I'm file source-2" 

Now run:

$ ./source.sh I'm file source-1 I'm file source-2 

If I'll change the call of the second file in first:

$ cat source.sh #!/bin/bash echo "I'm file source-1"  source source-2.sh 

It will have the same effect as using dot.

What is difference between these methods?

like image 741
setevoy Avatar asked Nov 20 '13 11:11

setevoy


People also ask

What is the difference between the DOT and source command?

source is a synonym for dot/period '. ' in bash, but not in POSIX sh, so for maximum compatibility use the period. When a script is run using source it runs within the existing shell, any variables created or modified by the script will remain available after the script completes.

What does running or sourcing a script mean?

To source a script is to run it in the context of the current shell rather than running it in a new shell. For example: . myscript.sh. or: source myscript.sh. (depending on which shell you're running).

What is the difference between executing a bash script vs sourcing it?

Sourcing a script will run the commands in the current shell process. Changes to the environment take effect in the current shell. Executing a script will run the commands in a new shell process.

What is the difference between source and dot in Linux?

On the other hand, source is 5 characters longer and is not portable to POSIX-only shells or Bourne whereas . (dot) is, so I never bother using source. That is correct - sourcing a file runs the commands in the current shell and it will affect your current shell environment.

What does the dot command do when I run a script?

When you run an executable script as ./my-script.sh, the commands are run in a new subshell, while when run as . my-script.sh the current shell context will be used. Generally, your current context is your terminal window. This mean that the dot command will apply changes to your current shell. Let’s look at the simple example below.

What is the difference between'source'and'(Dot)'?

They are equivalent in bash in that they do exactly the same thing. On the other hand, source is 5 characters longer and is not portable to POSIX-only shells or Bourne whereas . (dot) is, so I never bother using source. That is correct - sourcing a file runs the commands in the current shell and it will affect your current shell environment.

What is the difference between source and command in bash script?

The only difference is in portability. . is the POSIX-standard command for executing commands from a file; source is a more-readable synonym provided by Bash and some other shells. Bash itself, however, makes no distinction between the two. @MathieuCAROFF could you paste a simple script example?


2 Answers

The only difference is in portability.

. is the POSIX-standard command for executing commands from a file; source is a more-readable synonym provided by Bash and some other shells. Bash itself, however, makes no distinction between the two.

like image 140
chepner Avatar answered Oct 24 '22 14:10

chepner


There is no difference.

From the manual:

source

source filename  A synonym for . (see Bourne Shell Builtins). 
like image 41
devnull Avatar answered Oct 24 '22 14:10

devnull