I was testing a script and wanted to create a small file with a bit of text in it so I typed the following and forgot to escape the space in the filename:

echo "bob">other name

When I did ls, it showed the file "other" and when I did cat other, it showed the contents as:

bob name

How did "name" get in the file? I believe I saw somewhere that you can put redirection anywhere in a command. In fact, I just tested this:

echo >other Some text in a file

and "Some text in a file" was the contents of "other".

What is the purpose of this feature?

Of course, originally, I should have typed:

echo "bob">other\ name
link|improve this question

78% accept rate
feedback

1 Answer

up vote 8 down vote accepted

This is how bash redirection works.

Writing this:

echo "bob">other name

is equivalent to this:

echo "bob" name >other

redirection can appear anywhere on the line, the chevron (>) only applies to "other".

This allows people flexibility of how they wish to structure their commands. If it looks more clear doing something like this:

>file.txt echo "one" "two" "three"

then that is up to the person writing the script or command.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.