There are three main ways of running shell scripts in Linux:
1. Direct command release: The shell.sh file must have readable and operational (RX) permissions, and then:
Absolute path: Use/home/dmtsai/shell.sh to release commands;
Relative path: If the working directory is/home/dmtsai/, use./shell.sh to run
variable "path" function: Place the shell.sh in the directory where path refers fixed , for example: ~/bin/
2. Run as a bash program: Run through "bash shell.sh" or "sh shell.sh"
3. Run with the source shell.sh command
The difference: The first and the second are all running in the subroutine bash (Figure 1), and the third is running in the main program bash (Figure 2). When running in the first two ways, all the variables or actions created in the script will end without going back to the main program, and the third will add the variables to the main program bash.
Figure 1, sh02.sh running in a sub-program
Figure 2, sh02.sh running in the parent program
The difference between Shell script and different running modes