Linux下腳本的運(yùn)行方式有四種,以腳本test.sh為例:
第一種:bash test.sh
第二種:./test.sh
第三種:. test.sh
第四種:source test.sh
第一種和第二種都是開啟一個(gè)子進(jìn)程運(yùn)行腳本,但是第二種方式運(yùn)行腳本的前提是腳本有可執(zhí)行權(quán)限,需執(zhí)行命令chmod +x test.sh;
第三種和第四種都是在當(dāng)前進(jìn)程運(yùn)行腳本。
[root@docker1 lianxi]# cat test.sh # 查看腳本的內(nèi)容
i=1
# 輸出變量i
echo $i
# 輸出當(dāng)前的進(jìn)程號(hào)
echo $$
[root@docker1 lianxi]# ll test.sh # 查看腳本的權(quán)限,此時(shí)沒有可執(zhí)行權(quán)限
-rw-r--r-- 1 root root 20 11月 16 15:23 test.sh
[root@docker1 lianxi]# bash test.sh
1
7237
[root@docker1 lianxi]# ./test.sh
-bash: ./test.sh: 權(quán)限不夠
[root@docker1 lianxi]# . test.sh
1
7185
[root@docker1 lianxi]# source test.sh
1
7185
[root@docker1 lianxi]# chmod +x test.sh # 添加可執(zhí)行權(quán)限
[root@docker1 lianxi]# ll test.sh
-rwxr-xr-x 1 root root 20 11月 16 15:23 test.sh
[root@docker1 lianxi]# ./test.sh
1
7250