Why Bash redirects even when there's no command?

The Bash manual says that, after performing expansions

If no command name results, redirections are performed, but do not affect the current shell environment.

Now, what’s a use-case for this? What’s the point of redirecting if there’s nothing to do, and the redirection don’t even affect the current shell environment?

Asked By: Sebastian Carlos


You can always create files using just redirection:

> "$some_file"
Answered By: muru

One use-case is to create a file if it doesn’t exist:

>> foo

It’s also possible to ensure the file is empty:

> foo

Input redirections can serve as existence tests, but [ -f foo ] works just as well:

< foo
echo $?
Answered By: Stephen Kitt
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.