日积月累:Linux中的点命令(.)和source命令

来源:互联网 发布:快手喊麦都用什么软件 编辑:程序博客网 时间:2024/05/16 05:29
. filename [arguments]
source filename [arguments]

在当前shell环境中,读取并执行filename中的命令,返回最后一条命令的exit值。
在BASH中,点(.)和source命令都是BASH的内置命令,C-shells只支持source命令。

source或点(.)命令的用途:
重新执行初始化文件,如 .bash_profile 和 .profile等。.bash_profile和.bashrc无需可执行权限。
当需要改变当前shell的多个环境变量时,执行设置环境变量的脚本文件。这也是一种共享环境变量的方法。

示例:
[azureuser@vmname02 ~]$ ls -lrt...-rw-rw-r--. 1 azureuser azureuser   22 Oct  6 10:50 test.bash# 不必有执行权限-rw-rw-r--. 1 azureuser azureuser   26 Oct  6 11:10 test.tcsh# 不必有执行权限-rwxrwxr-x. 1 azureuser azureuser   27 Oct  6 11:22 test.sh# 需要执行权限,才能在shell命令行上执行[azureuser@vmname02 ~]$ cat test.sh# test.sh的内容echo $var[azureuser@vmname02 ~]$ cat test.bash# test.bash的内容var="hello"echo $var[azureuser@vmname02 ~]$ cat test.tcsh# test.tcsh的内容set var = helloecho $var[azureuser@vmname02 ~]$ var=hello# 在当前shell中,定义var变量[azureuser@vmname02 ~]$ echo $varhello[azureuser@vmname02 ~]$ ./test.sh# 产生一个新shell来执行脚本,变量var未定义[azureuser@vmname02 ~]$ [azureuser@vmname02 ~]$ export var=hello# export的变量只对当前shell和它的子shell有效[azureuser@vmname02 ~]$ ./test.shhello[azureuser@vmname02 ~]$ source test.bash# BASH支持source命令hello[azureuser@vmname02 ~]$ echo $var# var变量在当前shell中有效hello[azureuser@vmname02 ~]$ var=        # 重置变量[azureuser@vmname02 ~]$ echo $var[azureuser@vmname02 ~]$ . test.bash# BASH支持 . 命令hello[azureuser@vmname02 ~]$ echo $var# var变量在当前shell中有效hello[azureuser@vmname02 ~]$ /bin/tcsh -x# -x 执行前回显命令[azureuser@vmname02 ~]$ . test.tcsh# C-shells 不支持 . 命令. test.tcsh/usr/bin/.: Permission denied.[azureuser@vmname02 ~]$ source test.tcsh# C-shells 支持source命令source test.tcshset var = helloecho hellohello[azureuser@vmname02 ~]$ echo $var# var变量在当前shell中有效echo hellohello


参考:

http://linuxmanpages.com/man1/source.1.php
http://www.linuxidc.com/Linux/2012-10/72744.htm
0 0
原创粉丝点击