简体   繁体   English

在 bash 中创建空文件的简写在 zsh 中无法正常工作

[英]shorthand for creating empty file in bash not working properly in zsh

In bash, an empty file can be created with:在 bash 中,可以使用以下命令创建一个空文件:

> empty_file.txt

In zsh, the same command gets stuck and needs to be stopped.在 zsh 中,同样的命令卡住了,需要停止。 The file gets created nonetheless.尽管如此,文件还是会被创建。

I would like to know what is causing this behaviour and/or how to make it similar to bash?我想知道是什么导致了这种行为和/或如何使它类似于 bash?

Edit: typo.编辑:错别字。

If you don't specify a command for your redirection, then Bash will behave (accordingly to POSIX) as if the command was true .如果您没有为重定向指定命令,那么 Bash 的行为(根据 POSIX)就好像该命令为true By default, unless in compatibility mode, zsh will instead behave as if the command was cat .默认情况下,除非在兼容模式下,否则zsh行为就像命令是cat

You can simply explicitly specify the command true (or its harder-to-read alias : ) to make both shells behave the same:您可以简单地明确指定命令true (或其难以阅读的别名: )以使两个 shell 的行为相同:

: > empty_file.txt

Here's man zsh explaining the behavior and the options you can use to tweak it:这是man zsh解释行为和您可以用来调整它的选项:

   REDIRECTIONS WITH NO COMMAND

   When a simple command consists of one or more redirection operators
   and  zero  or  more parameter assignments, but no command name, zsh
   can behave in several ways.

   If the parameter NULLCMD is not set or the  option  CSH_NULLCMD  is
   set,  an error is caused.  This is the csh behavior and CSH_NULLCMD
   is set by default when emulating csh.

   If the option SH_NULLCMD is set, the builtin `:' is inserted  as  a
   command with the given redirections.  This is the default when emu‐
   lating sh or ksh.

   Otherwise, if the parameter NULLCMD is set, its value will be  used
   as  a  command  with  the  given redirections.  If both NULLCMD and
   READNULLCMD are set, then the value of the latter will be used  in‐
   stead  of that of the former when the redirection is an input.  The
   default for NULLCMD is `cat' and for READNULLCMD is `more'. Thus

          < file

   shows the contents of file on standard output, with paging if  that
   is  a  terminal.   NULLCMD and READNULLCMD may refer to shell func‐
   tions.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM