A009: shell 脚本各种执行方式(source ./*.sh, . ./*.sh, ./*.sh)的区别
2014-09-01 14:02
375 查看
原文出处:http://blog.csdn.net/dance_rise/article/details/8573560
结论一: ./*.sh的执行方式等价于sh ./*.sh或者bash ./*.sh,此三种执行脚本的方式都是重新启动一个子shell,在子shell中执行此脚本。
结论二: .source ./*.sh和 . ./*.sh的执行方式是等价的,即两种执行方式都是在当前shell进程中执行此脚本,而不是重新启动一个shell 而在子shell进程中执行此脚本。
验证依据:没有被export导出的变量(即非环境变量)是不能被子shell继承的
验证结果:
[plain] view
plaincopy
[root@localhost ~]#name=dangxu //定义一般变量
[root@localhost ~]# echo ${name}
dangxu
[root@localhost ~]# cat test.sh //验证脚本,实例化标题中的./*.sh
#!/bin/sh
echo ${name}
[root@localhost ~]# ls -l test.sh //验证脚本可执行
-rwxr-xr-x 1 root root 23 Feb 6 11:09 test.sh
[root@localhost ~]# ./test.sh //以下三个命令证明了结论一
[root@localhost ~]# sh ./test.sh
[root@localhost ~]# bash ./test.sh
[root@localhost ~]# . ./test.sh //以下两个命令证明了结论二
dangxu
[root@localhost ~]# source ./test.sh
dangxu
[root@localhost ~]#
./*.sh sh ./*.sh bash ./*.sh | source ./*.sh . ./*.sh |
启动一个子shell | 在父shell环境 |
变量不与父shell环境交互 | 变量与父shell环境交互 |
结论二: .source ./*.sh和 . ./*.sh的执行方式是等价的,即两种执行方式都是在当前shell进程中执行此脚本,而不是重新启动一个shell 而在子shell进程中执行此脚本。
验证依据:没有被export导出的变量(即非环境变量)是不能被子shell继承的
验证结果:
[plain] view
plaincopy
[root@localhost ~]#name=dangxu //定义一般变量
[root@localhost ~]# echo ${name}
dangxu
[root@localhost ~]# cat test.sh //验证脚本,实例化标题中的./*.sh
#!/bin/sh
echo ${name}
[root@localhost ~]# ls -l test.sh //验证脚本可执行
-rwxr-xr-x 1 root root 23 Feb 6 11:09 test.sh
[root@localhost ~]# ./test.sh //以下三个命令证明了结论一
[root@localhost ~]# sh ./test.sh
[root@localhost ~]# bash ./test.sh
[root@localhost ~]# . ./test.sh //以下两个命令证明了结论二
dangxu
[root@localhost ~]# source ./test.sh
dangxu
[root@localhost ~]#
相关文章推荐
- shell 脚本各种执行方式(source ./*.sh, . ./*.sh, ./*.sh)的区别
- shell 脚本各种执行方式(source ./*.sh, . ./*.sh, ./*.sh)的区别
- 关于shell unix下,直接执行shell与sh 执行 或加 . 脚本 的区别及含义
- Linux shell脚本执行方式:source、sh、bash、./执行脚本的区别
- shell脚本的两种执行方式区别举例
- shell脚本的几种不同执行方式的区别实验
- shell脚本执行命令 . 和sh 和 source 和 ./ 的使用以及区别
- Linux下为可执行shell脚本文件(.sh),制作桌面启动快捷方式
- Linux执行shell脚本方式及区别&命令后台运行
- linux source、sh、bash、./ 执行shell脚本 区别
- shell脚本的执行方式及区别
- 转:linux执行shell脚本的方式及一些区别
- linux执行shell脚本的方式及一些区别
- Linux执行shell脚本方式及区别&命令后台运行
- linux执行shell脚本的方式及一些区别
- linux 执行shell脚本的方式及一些区别
- shell各种执行方式区别
- 多种shell脚本执行方式之间的区别
- 详解shell中source、sh、bash、./执行脚本的区别
- Linux执行shell脚本方式及区别&命令后台运行