silverwind
silverwind

Reputation: 3924

bash: redirecting standard input to a file

zsh has this handy functionality where one can pipe stdin to a file using

>file

What is the bash equivalent to this?

Upvotes: 1

Views: 88

Answers (2)

cdarke
cdarke

Reputation: 44364

See the post from @chepner for a full explanation of how zsh does this. The equivalent in bash is:

cat > file

Alarm bells ring whenever we see cat, it is often abused yet this is a legitimate use of the program. Strictly speaking only the redirection part > file is bash, cat itself is an independent program.

Upvotes: 4

chepner
chepner

Reputation: 531345

The meaning of > file in zsh depends on how certain options are set. If SH_NULLCMD is set, then it is equivalent to : > file, which would be the equivalent command in any sh-derived shell, bash included.

See man zshmisc under the heading REDIRECTIONS WITH NO COMMAND for more details.

UPDATE: I'm completely mistaken. Setting SH_NULLCMD changes the meaning of > file altogether, making > file equivalent to : > file in bash rather than : > file in bash being equivalent to the default behavior of > file in zsh.

As cdarke pointed out (and he should post the answer), the correct equivalent in bash is cat > file.

Upvotes: 3

Related Questions