法1、絕對路徑方式:shell
[root@Dasoncheng ~]# cat 2.sh #!/bin/bash pstree
[root@Dasoncheng ~]# /root/2.sh ├─sshd───sshd───bash───2.sh───pstree ##須要具備-x執行權限,由於這樣執行 沒有指定命令解釋器、則會使用腳本里面指定的命令解釋器(如#!/bin/bash)去執行該腳本. ##若是腳本里面也沒有指定,則會調用系統默認的shell解釋器(echo $SHELL)查看; ##經過pstree能夠看到,是單獨啓動了2.sh這個子shell來執行腳本里面的命令的;
法2、相對路徑方式:bash
[root@Dasoncheng ~]# ./2.sh ├─sshd───sshd───bash───2.sh───pstree ##同法一,就是路徑方式不一樣;
法3、bash命令調用:ssh
[root@Dasoncheng ~]# sh 2.sh ├─sshd───sshd───bash───sh───pstree ##這裏就是指定了命令解釋器bash,即單獨啓動了sh這個子shell來執行命令;
[root@Dasoncheng ~]# which sh /usr/bin/sh [root@Dasoncheng ~]# ll /usr/bin/sh lrwxrwxrwx 1 root root 4 Feb 24 2017 /usr/bin/sh -> bash [root@Dasoncheng ~]# ll /usr/bin/bas base64 basename bash bashbug bashbug-64 [root@Dasoncheng ~]# ll /usr/bin/bash -rwxr-xr-x 1 root root 960472 Dec 7 2016 /usr/bin/bash
法4、". 2.sh"點空格相對路徑 或者"./root/2.sh"點空格絕對路徑方式:函數
[root@Dasoncheng ~]# . 2.sh ├─sshd───sshd───bash───pstree ##這個及下面的絕對路徑,都是直接在當前shell環境下面 執行了shell腳本;(沒有建立子shell) ##其實". "點空格 就是調用了系統裏面的一個函數,這裏不講;
[root@Dasoncheng ~]# . /root/2.sh ├─sshd───sshd───bash───pstree
開子shell與不開子shell的區別就在於,環境變量的繼承關係,如在子shell中設置的當前變量,不作特殊通道處理的話,父shell是不可見的。
而在當前shell中執行的話,則全部設置的環境變量都是直接生效可用的。code