方法一:切换到shell脚本所在的目录(此时,称为工作目录)执行shell脚本:
-
cd /data/shell
-
./hello.sh
方法二:以绝对路径的方式去执行bash shell脚本:
/data/shell/hello.sh
方法三:直接使用bash 或sh 来执行bash shell脚本:
-
cd /data/shell
-
bash hello.sh
或
-
cd /data/shell
-
sh hello.sh
方法四:在当前的shell环境中执行bash shell脚本:
-
cd /data/shell
-
. hello.sh
或
-
cd /data/shell
-
source hello.sh
前三种方法执行shell脚本时都是在当前shell(称为父shell)开启一个子shell环境,此shell脚本就在这个子shell环境中执行。shell脚本执行完后子shell环境随即关闭,然后又回到父shell中。而方法四则是在当前shell中执行的。
参考文献:https://blog.csdn.net/kitty_landon/article/details/54136352?utm_source=copy