命令 | 执行 | 用途 | 文件要求 | 执行过程 | 脚本解释器 |
---|---|---|---|---|---|
source | source a.sh | 通常针对配置文件,执行source使生效,如.bashrc中export变量,则source .bashrc之后,当前terminal中可以使用该变量 | 针对 a.sh, 当前用户不需要执行权限 | 在当前shell内执行,a.sh中对变量的修改,影响当前terminal中变量 | |
sh | sh a.sh | 通常用户执行shell脚本 | 针对 a.sh, 当前用户不需要有执行权限 | 打开一个subshell运行,使用变量与当前terminal一致,a.sh中对变量的修改,不会影响当前terminal中变量 | sh表示脚本默认使用sh脚本解释器 |
./ | ./a.sh | 通常用户执行脚本,不仅仅shell脚本,python脚本等也可以 | 针对 a.sh, 当前用户有执行权限, chmod修改 | 打开一个subshell运行,使用变量与当前terminal一致,a.sh中对变量的修改,不会影响当前terminal中变量 | 根据脚本第一行#!/bin/bash 表明使用哪种解释器,可以是shell,可以是python |
ps:
修改.bashrc后,生效方法:
- 执行了source .bashrc
- 重新打开一个terminal就会生效。因为重新打开一个终端,会加载.bashrc文件
为什么sh a.sh不可以运行,./a.sh可以运行
a.sh 脚本如下:
#!/bin/bash
source ~/2.sh
sh a.sh报错:source: not found
因为sh a.sh使用的是sh解释器,是dash的,是不支持source的,bash才支持source
# ls -l `which sh`
lrwxrwxrwx 1 root root 4 8月 31 2018 /bin/sh -> dash
为了使sh支持bash,解决方法:
1、先删除 /bin/sh 的软连接
# rm -r /bin/sh
2、重新映射 /bin/sh
# ln -s /bin/bash /bin/sh
参考
https://www.cnblogs.com/pcat/p/5467188.html
https://www.zhihu.com/question/41441630
https://blog.youkuaiyun.com/stpeace/article/details/45567977
http://blog.chinaunix.net/uid-28458801-id-4537781.html?_t=t