Linux执行shell脚本的几种方式

方式一:先进入脚本所在目录,使用 ./ 执行脚本

cd /data/shell      //进入shell所在目录
./test.sh           //执行shell脚本

脚本所在目录可能不在执行程序默认的搜索路径(环境变量PATH)之列之中,可能会提示找不到要执行的脚本。加上 ./ 代表是在当前目录下执行该脚本。
使用 echo $PATH 命令查看环境变量PATH:
在这里插入图片描述
方式二:先进入脚本所在目录,使用 bash执行脚本,bash可以简写为sh

cd /data/shell      //进入shell所在目录
bash test.sh        //执行shell脚本
或 
cd /data/shell      //进入shell所在目录
sh test.sh          //执行shell脚本

方式三:使用脚本的绝对路径执行脚本

/data/shell/test.sh

方式四:先进入脚本所在目录,使用 source 或 . 执行脚本

cd /data/shell      //进入shell所在目录
source test.sh      //执行shell脚本
或 
cd /data/shell      //进入shell所在目录
. test.sh           //执行shell脚本

注意事项:前三种方式是先再当前shell即父shell中开启一个子shell环境,然后在子shell中执行脚本,子shell环境会在脚本执行完后自动关闭。而方式四是直接在当前shell即父shell中执行脚本,此时不会开启子shell环境。
使用 echo $SHELL 命令查看当前用户所用的shell
在这里插入图片描述
使用 grep username /etc/passwd 命令查看指定用户所用的shell
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_39387856/article/details/86522665