shell 执行脚本的 3 种方式

来源:互联网 发布:耶稣基督赞美网络诗歌 编辑:程序博客网 时间:2024/05/18 15:26

首先把工作目录切换到脚本所在的目录:
该脚本所在的目录为:/home/user
cd /home/user
脚本为:hello_shell.sh
脚本的内容为:
#!/usr/bin/env bash
echo “hello shell”

方法一

sh hello_shell.sh
或者
bash hello_shell.sh
注该该种方式执行不需要为脚本赋予执行权限
-rw-rw-r– hello_shell.sh

方法二

相对路径
./hello_shell.sh
绝对路径
/home/user/hello_shell.sh
该种方式需要为脚本授予可执行权限
chmod 764 hello_shell.sh
-rwxrw-r– hello_shell.sh

方法三

. hello_shell.sh
或者
source hello_shell.sh

注:

前两种方法和第三种方法的区别:
前两种方法执行 shell 脚本时都是在当前 shell 环境下又开了一个子 shell 环境,当脚本执行完后,子 shell 环境立刻就会关闭,而方法三是在当前 shell 环境下执行的。

原创粉丝点击