Linux

使用自定義 zshrc 啟動 zsh

  • September 7, 2019

我希望能夠使用類似於命令的自定義 rc 文件啟動 zsh:bash --rc-file /path/to/file

如果這是不可能的,那麼是否可以啟動 zsh,執行source /path/to/file,然後留在同一個 zsh 會話中?

注意:該命令zsh --rcs /path/to/file不起作用,至少對我不起作用……

編輯:總的來說,我希望能夠執行以下操作: ssh到遠端伺服器“example.com”,執行zshsource我的配置位於/path/to/file,全部在 1 個命令中。這是我一直在努力的地方,尤其是因為我不想覆蓋遠端機器上的任何配置文件。

從手冊頁:

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.

因此您應該能夠將環境變數設置ZDOTDIR為新目錄以讓 zsh 查找一組不同的點文件。

正如手冊頁所建議的那樣,RCS並且GLOBAL_RCS不是 rc 文件的路徑,因為您嘗試使用它們,而是您可以啟用或禁用的選項。因此,例如,該標誌--rcs將啟用該RCS選項,導致 zsh 從 rc 文件中讀取。您可以使用以下命令行標誌來啟用或禁用 zshRCSGLOBAL_RCS

 --globalrcs
 --rcs
 -d    equivalent to --no-globalrcs
 -f    equivalent to --no-rcs

要回答您的另一個問題:

是否可以啟動 zsh,執行“source /path/to/file”,然後留在同一個 zsh 會話中?

是的,根據上述說明,這很容易。只是執行zsh -d -f然後source /path/to/zshrc

引用自:https://unix.stackexchange.com/questions/131716