Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shorthand for creating empty file in bash not working properly in zsh

In bash, an empty file can be created with:

> empty_file.txt

In zsh, the same command gets stuck and needs to be stopped. The file gets created nonetheless.

I would like to know what is causing this behaviour and/or how to make it similar to bash?

Edit: typo.

like image 447
ooo Avatar asked Oct 29 '25 09:10

ooo


1 Answers

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.
like image 147
that other guy Avatar answered Nov 01 '25 03:11

that other guy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!