Question

I have a bash script say

#!/bin/bash
echo $0

then there is a difference between ./script.sh (output: ./script.sh) and source script.sh(output: -bash). I'm using sourcein some scripts but I'm not quite shure I always use it the right way.

What differences are there between these calls? (Is it, that ./ calls a subshell and source runs within the same shell?)

What other ways are there to call a subscript?

Was it helpful?

Solution

You are right:

source scriptname will execute the script in current shell

./scriptname will create a new shell and execute everything there.

also(not related with question though):

source scriptname and . scriptname are same but source scriptname and ./scriptname are not.

Otherways:

1) the extra one I mentioned above.

2) exec

3) within () in a script or cmdprompt
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top