Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does ZSH hang on empty redirection?

Tags:

bash

shell

zsh

To truncate a file in a unixy environment you would commonly use:

$ > file

In bash (also dash), the file is truncated (or created if it doesn't exist), and then the prompt returns. In zsh, the file is truncated/created, but it hangs. You have to Ctrl-C to be able to use the prompt again.

Redirecting non-empty output works without a problem:

zsh$ cat nonempty.file > file
zsh$

The question is:

Is this behaviour expected or not? Is zsh wrong?

like image 735
Jon Cairns Avatar asked Mar 21 '13 10:03

Jon Cairns


1 Answers

I'm not really a zsh user but seems like > file in zsh is the same as cat > file in bash. To create a new file or truncate it in zsh, use : > file. This also works for bash.


UPDATE:

Found this in zsh manual:

  • 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 emulating 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 instead of that of the former when the redirection is an input. The default for NULLCMD is cat and for READNULLCMD is more.

On my system, the default is:

$ echo $ZSH_VERSION
4.3.10
$ echo $NULLCMD
cat
$ echo $READNULLCMD
/usr/bin/pager
$
like image 108
pynexj Avatar answered Nov 09 '22 17:11

pynexj