sh和之间有什么区别source?
sh
source
source: source filename [arguments] Read and execute commands from FILENAME and return. The pathnames in $PATH are used to find the directory containing FILENAME. If any ARGUMENTS are supplied, they become the positional parameters when FILENAME is executed.
对于man sh:
man sh
NAME bash - GNU Bourne-Again SHell SYNOPSIS bash [options] [file] COPYRIGHT Bash is Copyright (C) 1989-2004 by the Free Software Foundation, Inc. DESCRIPTION Bash is an sh-compatible command language interpreter that executes commands read from the standard input or from a file. Bash also incorporates useful features from the Korn and C shells (ksh and csh). Bash is intended to be a conformant implementation of the IEEE POSIX Shell and Tools specification (IEEE Working Group 1003.2).
调用source(或其别名.)时,您将bash脚本加载并执行 到 当前 bash进程中。所以你可以
.
调用时sh,您将启动一个 fork (子进程或 子 进程),该 fork 运行新的会话/bin/sh,通常是的符号链接bash。在这种情况下,子脚本完成时将删除由子脚本设置的环境变量。
/bin/sh
bash
注意 :sh可能是到 另一个 外壳的符号链接。
例如,如果要通过特定方式更改 当前工作目录 ,则无法执行
$ cat <<eof >myCd2Doc.sh #!/bin/sh cd /usr/share/doc eof $ chmod +x myCd2Doc.sh
这不会达到您的期望:
$ cd /tmp $ pwd /tmp $ ~/myCd2Doc.sh $ pwd /tmp
因为 当前的工作目录 是环境的一部分,并且myCd2Doc.sh将在一个 subshell中 运行。
myCd2Doc.sh
但:
$ cat >myCd2Doc.source <<eof # Shell source file myCd2Doc() { cd /usr/share/doc } eof $ . myCd2Doc.source $ cd /tmp $ pwd /tmp $ myCd2Doc $ pwd /usr/share/doc
我写了一些 mycd 函数示例(基于 bash Associative Array的 bash完成)。 __
mycd
$SHLVL
$ cd /tmp printf %b '\43\41/bin/bash\necho This is level \44SHLVL.\n' >qlvl.sh $ bash qlvl.sh This is level 2. $ source qlvl.sh This is level 1.
$ cat <<eoqlvl2 >qlvl2.sh #!/bin/bash export startLevel echo This is level $SHLVL started:${startLevel:=$SHLVL}. ((SHLVL<5)) && ./qlvl2.sh eoqlvl2 $ chmod +x qlvl2.sh $ ./qlvl2.sh This is level 2 started:2. This is level 3 started:2. This is level 4 started:2. This is level 5 started:2. $ source qlv2.sh This is level 1 started:1. This is level 2 started:1. This is level 3 started:1. This is level 4 started:1. This is level 5 started:1.
$ sed '$a ps --sid $SID fw' qlvl.sh >qlvl3.sh $ chmod +x qlvl3.sh $ export SID $ read SID < <(ps ho sid $$) $ echo $SID $$ 8983 8983
(当前的 PID ($$== 进程ID )与 SID ( 会话ID )是相同的标识符。并非总是如此。)
$$
$ ./qlvl3.sh This is level 2. PID TTY STAT TIME COMMAND 8983 pts/10 Ss 0:00 /bin/bash 10266 pts/10 S+ 0:00 \_ /bin/bash ./qlvl3.sh 10267 pts/10 R+ 0:00 \_ ps --sid 8983 fw $ . qlvl3.sh This is level 1. PID TTY STAT TIME COMMAND 8983 pts/10 Ss 0:00 /bin/bash 10428 pts/10 R+ 0:00 \_ ps --sid 8983 fw
点.是的别名source。因此,两个命令之间的唯一区别被 slash 替换为 space 。
slash
space
$ printf %b '\43\41/bin/bash\necho Ending this.\nsleep 1;exit 0\n' >finalTest.sh $ bash finalTest.sh Ending this. $ source finalTest.sh Ending this.
…您可能会注意到两种语法之间的行为 有所不同 。;-)