hjk*_*atz 26 linux shell zsh oh-my-zsh
我希望能够使用类似于命令的自定义 rc 文件启动 zsh: bash --rc-file /path/to/file
如果这是不可能的,那么是否可以启动 zsh,运行source /path/to/file,然后保持在同一个 zsh 会话中?
注意:该命令zsh --rcs /path/to/file不起作用,至少对我来说不起作用......
编辑:总的来说,我希望能够执行以下操作:
ssh到远程服务器“example.com”,运行zsh,source我的配置位于/path/to/file,all in 1 命令。这是我一直在努力的地方,特别是因为我不想在远程机器上写任何配置文件。
jay*_*ren 24
从手册页:
STARTUP/SHUTDOWN FILES
Commands are first read from /etc/zshenv; this cannot be overridden. Subsequent be?
haviour is modified by the RCS and GLOBAL_RCS options; the former affects all startup
files, while the second only affects global startup files (those shown here with an
path starting with a /). If one of the options is unset at any point, any subsequent
startup file(s) of the corresponding type will not be read. It is also possible for
a file in $ZDOTDIR to re-enable GLOBAL_RCS. Both RCS and GLOBAL_RCS are set by
default.
Commands are then read from $ZDOTDIR/.zshenv. If the shell is a login shell, com?
mands are read from /etc/zprofile and then $ZDOTDIR/.zprofile. Then, if the shell is
interactive, commands are read from /etc/zshrc and then $ZDOTDIR/.zshrc. Finally, if
the shell is a login shell, /etc/zlogin and $ZDOTDIR/.zlogin are read.
When a login shell exits, the files $ZDOTDIR/.zlogout and then /etc/zlogout are read.
This happens with either an explicit exit via the exit or logout commands, or an
implicit exit by reading end-of-file from the terminal. However, if the shell termi?
nates due to exec'ing another process, the logout files are not read. These are also
affected by the RCS and GLOBAL_RCS options. Note also that the RCS option affects
the saving of history files, i.e. if RCS is unset when the shell exits, no history
file will be saved.
If ZDOTDIR is unset, HOME is used instead. Files listed above as being in /etc may
be in another directory, depending on the installation.
As /etc/zshenv is run for all instances of zsh, it is important that it be kept as
small as possible. In particular, it is a good idea to put code that does not need
to be run for every single shell behind a test of the form `if [[ -o rcs ]]; then
...' so that it will not be executed when zsh is invoked with the `-f' option.
Run Code Online (Sandbox Code Playgroud)
所以你应该能够将环境变量设置ZDOTDIR到一个新目录,让 zsh 查找一组不同的点文件。
正如手册页所建议的那样,RCS并且GLOBAL_RCS不是 rc 文件的路径,因为您正在尝试使用它们,而是您可以启用或禁用的选项。因此,例如,该标志--rcs将启用该RCS选项,导致 zsh 从 rc 文件中读取。您可以使用以下命令行标志来 zsh 启用或禁用RCS或GLOBAL_RCS:
--globalrcs
--rcs
-d equivalent to --no-globalrcs
-f equivalent to --no-rcs
Run Code Online (Sandbox Code Playgroud)
回答你的另一个问题:
是否可以启动 zsh,运行“source /path/to/file”,然后保持在同一个 zsh 会话中?
是的,根据上述说明,这很容易。只需运行zsh -d -f然后source /path/to/zshrc。
而使用 ZDOTDIR,您可以告诉zsh解释.zshrc在您选择的任何目录中调用的文件,让它解释您选择的任何文件(不一定称为.zshrc)证明非常困难。
在sh或ksh仿真中,zsh评估$ENV; 所以你可以emulate zsh在你的顶部添加/path/to/file并执行:
ssh -t host 'zsh -c "ARGV0=sh ENV=/path/to/file exec zsh"'
Run Code Online (Sandbox Code Playgroud)
另一种非常复杂的方法可能是:
ssh -t host 'PS1='\''${${functions[zsh_directory_name]::="
set +o promptsubst
unset -f zsh_directory_name
unset PS1
. /path/to/file
"}+}${(D):-}${PS1=%m%# }'\' exec zsh -o promptsubst -f
Run Code Online (Sandbox Code Playgroud)
那一点值得解释一下。
${foo::=value}是一个实际设置 的变量扩展$foo。$functions是一个特殊的关联数组,它将函数名称映射到它们的定义。
使用该promptsubst选项,$PS1将扩展in中的变量。因此,在第一个提示时,将扩展该 PS1 中的变量。
该zsh_directory_name函数是一个特殊的函数,有助于扩展~footo/path/to/something和 reverse。例如,%~在提示中使用它,以便如果当前目录是,/opt/myproj/proj/x您可以~proj:x通过zsh_directory_name执行映射proj:x<=>来显示它/opt/myproj/proj/x。这也被D参数扩展标志使用。所以如果一个 expand ${(D)somevar},该zsh_directory_name函数将被调用。
在这里,我们使用${(D):-}, ${:-},即${no_var:-nothing}扩展为nothingif$no_var为空,因此${(D):-}在调用时扩展为空zsh_directory_name。zsh_directory_name之前被定义为:
zsh_directory_name() {
set +o promptsubst
unset -f zsh_directory_name
unset PS1; . /path/to/file
}
Run Code Online (Sandbox Code Playgroud)
也就是说,在第一次 PS1 扩展时(在第一个提示时),${(D):-}将导致promptsubst选项未设置(取消-o promptsubst)、zsh_directory_name()未定义(因为我们只想运行它一次)$PS1、未设置和/path/to/file来源。
${PS1=%m%# }扩展(并分配$PS1)到%m%#除非 PS1 已经定义(例如在/path/to/file之后unset),并且%m%#恰好是 的默认值PS1。