If you don’t specify a command for your redirection, then Bash will behave (accordingly to POSIX) as if the command was true. By default, unless in compatibility mode, zsh will instead behave as if the command was cat.

You can simply explicitly specify the command true (or its harder-to-read alias :) to make both shells behave the same:

: > empty_file.txt

Here’s man zsh explaining the behavior and the options you can use to tweak it:

   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.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top