- 论坛徽章:
- 1
|
原帖由 FH 于 2007-1-2 08:33 发表
当bash以sh为名运行时,在交互非login模式下,其继承了所有环境变量(包括任何用户定义的变量),唯独不继承PS*!
号称是靠向POSIX,但是俺用过的所有Unix(AIX、HPUX、SCO)的sh,在该模式下都是继承PS*变量的。 ...
不知道 FH 所定義的“交互非login模式”跟我們所理解的是否一致的?還是有特定所指?
如下是 man bash 的內容:
A login shell is one whose first character of argument zero is a -, or
one started with the --login option.
An interactive shell is one started without non-option arguments and
without the -c option whose standard input and error are both connected
to terminals (as determined by isatty(3)), or one started with the -i
option. PS1 is set and $- includes i if bash is interactive, allowing
a shell script or a startup file to test this state.
由此看來,只要完成 login 只後,在 command line 輸入 bash 或 sh 都應該是“交互非login模式”囉。
上面有提到 PS1 會被設定,我猜,這裡的設定應該是會被“重設”。而兄的問題在於 PS1 是否會從 parent shell 繼承下來吧? 如果我猜得對,那你看到的現象是正常的。
再看 man:
When an interactive shell that is not a login shell is started, bash
reads and executes commands from ~/.bashrc, if that file exists. This
may be inhibited by using the --norc option. The --rcfile file option
will force bash to read and execute commands from file instead of
~/.bashrc.
所以,除了從 parent 的 env/export 所繼承外,~/.bashrc 也是會影響環境結果的。
但不同的 linux distro 都會去修改 /etc/skel/.bashrc 的內容,
這時,最好請兄台說一下你目前试用的版本,還有 ~/.bashrc 的實際代碼才好分析。
有些版本,會從 ~/.bashrc 去 source /etc/bashrc,然後再去 source /etc/profile.d/*.sh 。
而有些則不一定會。
而 FH 兄前面的所謂解決方法,就是在 /etc/profile.d/*.sh 裡去執行,那是可以的。
不過,上面是用 bash 做 command ,如果換成 sh 的話:
If bash is invoked with the name sh, it tries to mimic the startup
behavior of historical versions of sh as closely as possible, while
conforming to the POSIX standard as well. When invoked as an interac‐\r
tive login shell, or a non-interactive shell with the --login option,
it first attempts to read and execute commands from /etc/profile and
~/.profile, in that order. The --noprofile option may be used to
inhibit this behavior. When invoked as an interactive shell with the
name sh, bash looks for the variable ENV, expands its value if it is
defined, and uses the expanded value as the name of a file to read and
execute. Since a shell invoked as sh does not attempt to read and exe‐\r
cute commands from any other startup files, the --rcfile option has no
effect. A non-interactive shell invoked with the name sh does not
attempt to read any other startup files. When invoked as sh, bash
enters posix mode after the startup files are read.
When bash is started in posix mode, as with the --posix command line
option, it follows the POSIX standard for startup files. In this mode,
interactive shells expand the ENV variable and commands are read and
executed from the file whose name is the expanded value. No other
startup files are read.
所以看來是有 $ENV 這個變數來決定要執行的 script。
如果 ENV 沒設定,那就不執行了。
以我的 openSuSE10.2(GNU bash, version 3.1.17(1)-release )為例,
ENV 是設為 /etc/bash.bashrc 。裡面有多行關於 PS1 的設定,因此也會影響實際的 prompt 結果。
結論就是:不同的發行版本,可能得到的結果都不盡相同,只能以實際的 initial script 作參考。 |
|