In bash you can place redirections almost anywhere in the command. All these are equivalent:

$ > file command arg1 arg2 arg3
$ command > file arg1 arg2 arg3
$ command arg1 > file arg2 arg3
$ command arg1 arg2 > file arg3
$ command arg1 arg2 arg3 > file

Try it yourself:

$ > file echo hello
$ echo > file hello
$ echo hello > file

Pretty cool, huh?

Comments

September 07, 2014, 14:12

This also works ash, ksh, tcsh and zsh. Probably works in all sh-compatible shells.

I found an explanation in the ash's documentation:

Simple Commands
If a simple command has been recognized, the shell performs the following actions:

1. Leading words of the form ``name=value'' are stripped off and assigned to the environment of the simple command. Redirection operators and their arguments (as described below) are stripped off and saved for processing.

2. The remaining words are expanded as described in the section called ``Expansions'', and the first remaining word is consid- ered the command name and the command is located. The remain- ing words are considered the arguments of the command. If no command name resulted, then the ``name=value'' variable assignments recognized in item 1 affect the current shell.

3. Redirections are performed as described in the next section.

Simon Permalink
September 07, 2014, 21:01

They might all work, though a lot of them are probably bad practice as far as readability is concerned. Putting the redirection in the middle of the parameters... that's just evil...

Darko Permalink
October 29, 2014, 11:24

Exactly the same thing went through my mind, most of these are pure evil :)

February 20, 2015, 03:02

You can also use '> new_file' with no program name as a shorter replacement for 'touch new_file'

Leave a new comment

(why do I need your e-mail?)

(Your twitter handle, if you have one.)

Type the word "quake3_418": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.

Advertisements