四种方式: shell
. script # using the . (dot) source script # using the `source` command sh /path/to/script # using the `sh` command /path/to/script # using the path (absolute or relative)
说明:bash
. (点)和 source不彻底等价。.(点)仅在bash类shell能正常工做,source在bash类和csh类shell都能正常工做。它们共同点是都会就地执行脚本,换句话说,该脚本的全部函数和非本地变量都会保留。举个例子,若是被执行的脚本经过cd命令进入了一个目录,脚本执行完毕后,你仍然处于这个目录。函数
另外两种方法使用了指定的解析命令。如无明示,将会使用脚本首行指定的默认解析命令。它们的共同点是不保留脚本内的变量。亦即不影响调用时的环境。code