Shell scripts can be executed in the following ways:
Way One:
./script.sh # using a decimal point to perform
Way two:
SH script.sh or bash script.sh # using bash (SH) to execute scripts
The previous two methods are all the same: they are executed in a child process under the current parent process, and when the child process is complete, the variables or actions in the child process will end without being passed back to the parent process. For example:
As you can see, after execution, look at the two variables in the script that don't work in the parent process!
Note: If the script1.sh script is executed inside the script2.sh file, it is executed. ./script1.sh [There are spaces in the middle]
Way three:
SOURCE script.sh or. liqiang.sh# uses a decimal point to perform
The third way of executing (source test.sh) is that it works in the parent process:
This is the difference between direct execution and execution with the source command, which applies only to the child process itself, while the latter acts on the entire parent process.
Therefore: If you want to not unregister the system and have the global configuration file take effect, you must use the source command:
For example: In the global configuration file/etc/profile added Java_home, let him for the entire environment to take effect
Export java_home=/usr/java/jdk1.7.0_75
You have to do source/etc/profile.
Note: To assign permissions at execution time
chmod a+x liqiang.sh #赋权限chmod a+x script-name
Complementary points of knowledge:
A canonical shell script indicates in the first line which program (interpreter) to execute the contents of the script, which is typically programmed in Linux bash:
#! /bin/bash
Or
#! /bin/sh
Attention:
(1) In the shell, if the first letter of a line is #, then it is a comment, but the above two are written on the first line, so it is not a script comment line, and if it is written after a command, it becomes a comment line.
(2) SH is a soft link for bash, in most cases, the beginning of the script using "#!/bin/bash" and "#!/bin/sh" is no different, but the more canonical notation is to use "#!/bin/bash" at the beginning of the script.
Three ways to execute shell scripts