sh file executes a shell script file in a new shell process.
. file . file executes the shell script file in the current shell process.
./file will execute the file in the current directory. The file can be a binary executable file or it can start with the hashbang line (the first line of the file in the form #!.... , for example #!/usr/bin/ruby in the file means that the script should be executed as a Ruby file). An executable file flag must be set for the file.
For example, if you have a test.sh script:
and you run it with sh test.sh , you run a new sh (or rather bash , most likely, since it will be bound to another on modern systems), and then define a new variable in it, then exit. The next echo $TEST prints an empty string - the variable is not set in the outer shell.
If you run it with . test.sh . test.sh , you must execute the script using the current shell. The result of echo $TEST will print present .
If you run it with ./test.sh , the first line #!/bin/sh is detected, then it will be exactly as if you wrote /bin/sh ./test.sh , which in this case reduces to the first scenario. But if the hashbang line was, for example, #!/usr/bin/perl -w , the file would be executed using /usr/bin/perl -w ./test.sh .
Amadan
source share