Skip to content

Commit

Permalink
[doc/ref] Document the redirect to file behavior
Browse files Browse the repository at this point in the history
We have a bash- and zsh-compatible behavior.
  • Loading branch information
Andy C committed Sep 1, 2023
1 parent 9fd4445 commit 3410759
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions doc/ref/chap-cmd-lang.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,37 @@ The resulting PID is recorded in the `$!` variable.

### redir-file

Three variants of redirecting stdout:
Examples of redirecting the `stdout` of a command:

echo foo > out.txt # write to a file
echo foo >> out.txt # append to a file
echo foo > out.txt # overwrite out.txt
date >> stamp.txt # append to stamp.txt

<!--

This comment has been minimized.

Copy link
@bar-g

bar-g Sep 1, 2023

Contributor

Not bad to mention the >| only in a comment. I did get confused by it and noclobber when first encountered and used.
I came to think that it's good to know about noclobber, however, rather explained right-away as a feature for debugging output files, much less, if at all, to prevent accidental overwriting (it's not active everywhere, so fosters getting into a wrong routine, that deviates from the general default).

echo foo >| out.txt # clobber the file even if set -o noclobber
-->

Redirect stdin:
Redirect to the `stdin` of a command:

cat < in.txt

Redirects are compatible with POSIX and bash, so they take descriptor numbers
on the left:

make 2> stderr.txt # '2>' is valid, but '2 >' is not

Note that the word argument to **file** redirects is evaluated like bash, which
is different than other arguments to other redirects:

tar -x -z < Python* # glob must expand to exactly 1 file
tar -x -z < $myvar # $myvar is split because it's unquoted

In other words, it's evaluated **as** a sequence of 1 word, which **produces**
zero to N strings. But redirects are only valid when it produces exactly 1
string.

(Related: YSH uses `shopt --set simple_word_eval`, which means that globs that

This comment has been minimized.

Copy link
@bar-g

bar-g Sep 1, 2023

Contributor

Isn't shopt -s nullglob more specific?

match nothing evaluate to zero strings, not themselves.)

<!-- They also take a file descriptor on the left -->


Expand Down

0 comments on commit 3410759

Please sign in to comment.