跳转至

3. source与sh区别

1.3 source与sh的区别

[root@151 ~]# echo user1='chupeng' >test.sh
[root@151 ~]# echo $user1

[root@151 ~]# sh test.sh
[root@151 ~]# echo $user1

[root@151 ~]# source test.sh
[root@151 ~]# echo $user1
chupeng

# 可以看出:
    source  在当前shell环境中执行脚本,保留当前的shell变量
    sh      开启子shell环境执行脚本,不保留当前的shell变量
ubuntu@4c16g:~$ echo user1='chupeng' >test.sh
ubuntu@4c16g:~$
ubuntu@4c16g:~$ echo $user1

ubuntu@4c16g:~$
ubuntu@4c16g:~$ cat test.sh
user1=chupeng
ubuntu@4c16g:~$
ubuntu@4c16g:~$ sh test.sh
ubuntu@4c16g:~$ echo $user1

ubuntu@4c16g:~$
ubuntu@4c16g:~$ source test.sh
ubuntu@4c16g:~$ echo $user1
chupeng

最后更新: 2022-02-19 13:59:07