chmod +x test.sh
./test.sh

source命令:

source filepath/filename.sh ./filepath
功能:使当前shell读入路径为filepath的shell文件/filename.sh并依次执行文件中的所有语句

source filepath 与 sh filepath 、./filepath的区别:
当shell脚本具有可执行权限时,用sh filepath与./filepath是没有区别的。./filepath是因为当前目录没有在PATH中,所有"."是用来表示当前目录的。

sh filepath 会重新建立一个子shell,在子shell中执行脚本里面的语句,该子shell继承父shell的环境变量,但子shell是新建的,其改变的变量不会被带回父shell,除非使用export。
source filename其实只是简单地读取脚本里面的语句依次在当前shell里面执行,没有建立新的子shell。那么脚本里面所有新建、改变变量的语句都会保存在当前shell里面。

posted on 2017-11-30 18:55  E-C-Ares  阅读(168)  评论(0编辑  收藏  举报