我刚刚在我的 Ubuntu 系统上安装了 zsh。似乎 zsh 没有在 init 上执行 ~/.profile 。据我所知,这应该是一种自动行为。我错过了什么?
Gil*_*il' 323
.profile 对比 .zprofile当 Zsh作为登录 shell 被调用时,它运行~/.zprofile,而不是~/.profile。原因是 zsh 与标准 shell 有足够的不兼容性来破坏脚本。
~/.profile如果Zsh被调用为shor ,它确实会运行ksh。但是,如果您的目标是在登录时获得 zsh 提示,那也无济于事。
您可以制作/bin/sh您的登录外壳并将其包含export SHELL=/bin/zsh在您的~/.profile. 然后当你打开一个终端时,终端会启动zsh(除了少数终端模拟器不遵守$SHELL设置)。但是sh当您通过 ssh 登录时,您仍然会有。这可以通过exec zsh在结尾处包含来解决~/.profile(这会用 zsh 替换正在运行的 shell),但是您只需要在交互式登录时才这样做,而不是在~/.profile其他脚本(例如 X 会话启动)中包含时test 是通过 获得的父进程的名称ps -o comm= $PPID:如果它是sshd或su,那么它是安全的exec)。
使用 zsh 和 run 的最简单解决方案~/.profile是创建一个~/.zprofile在运行时进入 sh 仿真模式的文件~/.profile:
emulate sh
. ~/.profile
emulate zsh
Run Code Online (Sandbox Code Playgroud)
如果您最近有足够的 zsh(在 Ubuntu 上,我认为这意味着从 lucid 开始),您可以将其简化为emulate sh -c '. ~/.profile'.
.zprofile 对比 .zshrc该文件~/.profile由登录shell加载。登录 shell 是当您以文本模式登录时启动的第一个进程,例如在文本控制台上或通过 ssh。默认情况下,在大多数 Linux 机器上,登录 shell 是 bash,但您可以使用chsh命令或通过其他工具(例如 Ubuntu 中的“用户设置”)更改它。当它是登录 shell 时,bash 读取~/.bash_profile它是否存在和~/.profile,而 zsh 只读取~/.zprofile(因为它的语法与传统的 sh 不完全兼容)。在大多数配置下,~/.profile当您登录图形显示管理器时,也由 X 会话启动脚本加载。
当您启动终端模拟器并获得 shell 提示时,或者当您显式启动 shell 时,您将获得一个不是登录 shell 的 shell。由于~/.profile(或~/.zprofile) 用于登录时要执行的命令,因此非登录 shell 不会读取此文件。相反,当您启动交互式 zsh 时,它会读取~/.zshrc. (zsh 读~/.zshrc入所有交互式 shell,无论它们是登录 shell 都不是;奇怪的是,bash 从不读~/.bashrc入登录 shell。)
通常,~/.profile包含环境变量定义,并且可能会启动一些您想在登录时运行一次或整个会话的程序;~/.zshrc包含每个shell实例必须做的事情,例如别名和函数定义、shell选项设置、完成设置、提示设置、键绑定等。
Kar*_*son 141
~/.profilezsh登录时未加载。zsh~/.zprofile登录时加载。zsh~/.zshrc开始新的终端会话时加载。需要更多信息?看看吉尔斯的精彩回答!
小智 27
除了 Gilles 的回答之外,使用最新版本的 zsh,您还可以执行以下操作:
[[ -e ~/.profile ]] && emulate sh -c 'source ~/.profile'
Run Code Online (Sandbox Code Playgroud)
...这将在 zsh 的 sh-mode 生效的情况下获取 .profile 文件。并且它仅在源期间处于活动状态。因此,您无需保存当前选项状态即可在采购后再次播放。
小智 8
我.zprofile兼容.profile(仅包含 PATH 更改),因此只需要:
ln -s .profile .zprofile
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
172506 次 |
| 最近记录: |