没有这样的事件:1

时间:2017-05-30 05:10:07

标签: zsh

我有以下$HOME/.zshrc文件:

[vagrant@devel]/vagrant% cat ~/.zshrc
#!/usr/bin/env zsh
# BEGIN ANSIBLE MANAGED BLOCK
if [ $(history | wc -l) -eq 0 ]; then
  # we've just shelled in; "magically" cd into the vagrant shared folder
  cd "/vagrant"
fi
# END ANSIBLE MANAGED BLOCK

我使用与Bash相同的脚本,它运行得很好;在初次登录时,用户没有历史记录,并且被神奇地转移到/vagrant

当我使用此$HOME/.zshrc登录此框时,我看到以下错误:

/home/vagrant/.zshrc:fc:3: no such event: 1
[vagrant@devel]/vagrant%

我不知道这意味着什么,Google也没有告诉我结果。显然代码有效,但这似乎是某种错误。

有什么想法吗?

2 个答案:

答案 0 :(得分:2)

您无需调用history内置命令并计算行数。

您只需在HISTCMD中检查~/.zshrc变量为零即可。 HISTCMD表示历史记录中的当前命令序列号。

所以你的~/.zshrc就是这样:

# BEGIN ANSIBLE MANAGED BLOCK
if [[ $HISTCMD -eq 0 ]]; then
  # we've just shelled in; "magically" cd into the vagrant shared folder
  cd "/vagrant"
fi
# END ANSIBLE MANAGED BLOCK

答案 1 :(得分:0)

显然,zsh的{​​{1}}命令在没有历史记录时会发出错误,因此:

history

不再有错误。