有没有一种方法可以在zsh中禁用交互模式?

4
在zsh中,有没有一种方法可以在已经交互引用的脚本中关闭交互模式(特别是别名)?
例如,如果我在foo.zsh中定义:
#!/bin/zsh

a ha

然后执行

alias a=echo
./foo.zsh

我遇到了一个错误,因为别名未被应用;但如果我执行以下操作:
. ./foo.zsh

我拿到了ha
foo.zsh中是否有一种方法可以禁用别名a,即使它已经被使用.引用了?

1
运行脚本的 shell 总是非交互式的。尽管如此,脚本仍然可以访问其 tty。甚至可以在脚本中模拟交互式 shell - 来源。从这个答案来看,您正在尝试在当前 shell 中运行脚本,同时又不想将脚本放在当前 shell 中。使用 source 是绝对必要的吗?您想做什么(这是一个学习练习还是需要 source 的任务)? - simont
@simont 我正在使用Sebastian Celis的git提示符,但是偶尔我会通过在我的.zshrc中定义新别名来破坏它。 - Owen
1个回答

1
这在手册中已经相当清晰地记录了:

INTERACTIVE (-i, ksh: -i)
     This is an interactive shell.  This option is set upon
     initialisation if the standard input is a tty and commands are
     being read from standard input.  (See the discussion of
     SHIN_STDIN.)  This heuristic may be overridden by specifying a
     state for this option on the command line.  The value of this
     option can only be changed via flags supplied at invocation of the
     shell.  It cannot be changed once zsh is running.

但是您不需要关闭交互模式来防止别名扩展:
$ setopt no_aliases
$ . ./foo.zsh
./foo.zsh:3: command not found: a
$ setopt aliases   
$ . ./foo.zsh   
ha

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接