I am reading an article on executing bash shell scripts.
Method 1: Create a directory for your shell scripts and add the directory to the contents of the PATH variable, so that you can execute the script as shown below. When this executes, the variables, functions and aliases created in this subshell are only known to the particular bash session of that subshell. When that shell exits and the parent regains control, everything is cleaned up and all changes to the state of the shell made by the script, are forgotten.
$ hello_world.sh
Method 2: A script can also explicitly be executed by a given shell. The specified shell will start as a subshell of your current shell and execute the script.
$ sh script_name.sh
Method 3: If you don't want to start a new shell but execute the script in the current shell, you source it:
source script_name.sh
I really don't understand the benefits and disadvantages of each of these methods. Can anyone clarify?
./this/is/my/script.sh
. Furthermore you can create an alias to the full path so that you need not modify$PATH
. And after exiting a subshell nothing is cleaned up: The calling shell's environment never was affected. – Hauke Laging Mar 14 '14 at 0:08