Why use > at beginning of command line
Moderator: DosItHelp
-
- Posts: 55
- Joined: 11 Dec 2017 09:08
Why use > at beginning of command line
@aGerman does that here. That's the first time I've seen it in that sequence, and I can't find it documented.
I suppose I am wondering what is the difference between
echo > a.txt
which is how I always thought redirection was done, and
> a.txt echo
which seems to yield similar or identical results in my tests. (All I can imagine is that it might save a pair of parentheses when using something more complex than just echo)
I suppose I am wondering what is the difference between
echo > a.txt
which is how I always thought redirection was done, and
> a.txt echo
which seems to yield similar or identical results in my tests. (All I can imagine is that it might save a pair of parentheses when using something more complex than just echo)
Re: Why use > at beginning of command line
1) Single digits are treated as stream specifiers (0 for StdIn, 1 for StdOut, 2 for StdErr, 3-9 for user-defined streams).
redirects Echo is off to the file.
redirects 1 to the file plus the trailing space between 1 and >.
redirects only the 1 to the file as expected.
2) It improves the readability of the code.vs.
Steffen
Code: Select all
echo 1>"test.txt"
Code: Select all
echo 1 >"test.txt"
Code: Select all
>"test.txt" echo 1
2) It improves the readability of the code.
Code: Select all
echo Hello!>"test.txt"
echo That's just a test.>>"test.txt"
echo Bye>>"test.txt"
Code: Select all
>"test.txt" echo Hello!
>>"test.txt" echo That's just a test.
>>"test.txt" echo Bye
-
- Posts: 55
- Joined: 11 Dec 2017 09:08
Re: Why use > at beginning of command line
Thank you. That makes sense and your explanation is very logical to follow, and contains information I did not know.
For readability I sometimes use whitespace to align commands, like (if this shows up the same for you as it does for me on the forum post)
but as you point out, when doing this with echo, those extras blanks are undesirably redirected! So I should consider the rearranged way (or use parentheses) instead.
(EDIT it's not showing up correctly because the forum is eating whitespace. There are 7 additional spaces after the 1 in the first echo shown, and another space after the single >, which in a nonproportional font display would have lined up nicely, although with the issue of echo not treating them as whitespace.)
For readability I sometimes use whitespace to align commands, like (if this shows up the same for you as it does for me on the forum post)
Code: Select all
echo 1 > foo
echo 12345678 >> foo
(EDIT it's not showing up correctly because the forum is eating whitespace. There are 7 additional spaces after the 1 in the first echo shown, and another space after the single >, which in a nonproportional font display would have lined up nicely, although with the issue of echo not treating them as whitespace.)
Last edited by aGerman on 26 Dec 2017 14:12, edited 2 times in total.
Reason: code tags added
Reason: code tags added
Re: Why use > at beginning of command line
Which is the reason why you should use code tags (the </> button in the editor box).MicrosoftIsKillingMe wrote: ↑26 Dec 2017 13:59(EDIT it's not showing up correctly because the forum is eating whitespace. ...
Steffen
-
- Posts: 55
- Joined: 11 Dec 2017 09:08
Re: Why use > at beginning of command line
Sorry - I only avoided them because I incorrectly thought that the code tags would eat whitespace, instead of the other way around. (I think some types of code tags on programming forums do that, anyway.) No excuse though - all I had to do was try it with Preview. Thanks for the fix. (Actually, the non-tagged DID look correct in preview, though, thus my EDIT remark )
-
- Posts: 128
- Joined: 23 May 2016 15:39
- Location: Spain
Re: Why use > at beginning of command line
just a side note, any time a redirection is used within a block, the system opens and closes the file (or stream).
but
Opens the file/stream, processes all commands in the block, and then close the file/stream, so the file is opened and closed only once. This may improve performance, specially with large files.
BTWis the same as
Some people prefer the first one for input files, and the second for output files. It's only a choice.
Also, to echo to screen in a redirected block, just redirect desired screen output to >con
Code: Select all
for %%I in .... (
>>"somefile.txt" echo %%I
)
Code: Select all
>>"somefile.txt" (
for %%I in .... (
echo %%I
)
)
BTW
Code: Select all
>>"%LOGFILE%" (
...
...
)
Code: Select all
(
...
...
)>>"%LOGFILE%"
Also, to echo to screen in a redirected block, just redirect desired screen output to >con
Code: Select all
>"%LOGFILE%" (
...
echo line_1
echo line_2
echo line_3
>CON echo This goes to screen, the others to the stream
...
)
-
- Posts: 55
- Joined: 11 Dec 2017 09:08
Re: Why use > at beginning of command line
elzooilogico, those are things that I had not even considered, so your post is helpful to me - on each point - so thank you for the additional explanations.
Re: Why use > at beginning of command line
I suppose those are some reason to do it but when considering readability it's worth considering how convention can make things easier to recognise/read.
I think there are many reasons why putting it afterwards is clearer..
A)The convention most people use is putting it afterwards.. the first time I ever saw it before was this forum..
B)With the arrow of > or <, you get the sense that what is on the side of the bigger part of the arrow is coming in and what is on the pointy side of the arrow is where it's going and it indicates an order 'cos the fat side of the arrow has what's coming in which is the first thing and the pointy side has where it's going. And if the arrow sits between the command and the file and points, then it indicates whether it's input or output in a very readable way.
C)And in English and Maths we tend to think in terms of e.g. for english, subject verb object. or in mathematics, infix operators so- operand operator operand 3+5, rather than +(3,5). There are times when looking at something in prefix can arguably be clearer like /(7,3)=(2,1) i.e (7 / 3 = 2 remainder 1). But some operators are much clearer as infix, like x>5 to indicate that x is greater than 5, is visually a bit clearer than >(x,5) or G(x,5). The fat and pointy side of > and < are useful with indicating input/output too when the symbol is infix.
In the case of echo, then sure you lose a little readability because there's an issue of whitespace mattering. so you do echo asdf>blah. But since echo is so simple, and it's so common to echo something and output to a file, that it's still pretty easy to read.
I think there are many reasons why putting it afterwards is clearer..
A)The convention most people use is putting it afterwards.. the first time I ever saw it before was this forum..
B)With the arrow of > or <, you get the sense that what is on the side of the bigger part of the arrow is coming in and what is on the pointy side of the arrow is where it's going and it indicates an order 'cos the fat side of the arrow has what's coming in which is the first thing and the pointy side has where it's going. And if the arrow sits between the command and the file and points, then it indicates whether it's input or output in a very readable way.
C)And in English and Maths we tend to think in terms of e.g. for english, subject verb object. or in mathematics, infix operators so- operand operator operand 3+5, rather than +(3,5). There are times when looking at something in prefix can arguably be clearer like /(7,3)=(2,1) i.e (7 / 3 = 2 remainder 1). But some operators are much clearer as infix, like x>5 to indicate that x is greater than 5, is visually a bit clearer than >(x,5) or G(x,5). The fat and pointy side of > and < are useful with indicating input/output too when the symbol is infix.
In the case of echo, then sure you lose a little readability because there's an issue of whitespace mattering. so you do echo asdf>blah. But since echo is so simple, and it's so common to echo something and output to a file, that it's still pretty easy to read.
Re: Why use > at beginning of command line
The issue with a single digit at the end of a line is still valid. And it's just a matter of habit how you read something. You're complaining because the direction western people read is left to right. True. But would you ever consider SET A=B is meant that A is assigned to B? You're used to read the assignment right to left. That's it. In terms of best practice it's sometimes worth to give up a habit.
Steffen
Steffen
-
- Posts: 55
- Joined: 11 Dec 2017 09:08
Re: Why use > at beginning of command line
Just to illustrate the other option for trailing space elimination with parentheses,
It is already obvious to some of you, but note that the following do not produce the same result
as the second of this pair redirects the extra space before the 1, as well as any trailing spaces after the 1. So parentheses may still be useful or add clarity in this sequence.
If you took
and "cut" the first 7 bytes including the space after the 1 to form
then you would still have the trailing space that is redirected.
aGerman, so there's no misunderstanding, I'm neither disagreeing nor complaining with anything you said. These are merely illustrations for clarity.
When testing variations of these examples be alert that, as pointed out earlier by aGerman, the consecutive sequence 1> has a different interpretation.
taripo, my sequential comprehension process is exactly in line with your description - you articulated it well - and accordingly I have no intuitive flow with the other sequence; but I'm going to try to be open to the possibility that my habit might sometimes conflict with best practice.
Code: Select all
echo 1 > bad.txt
(echo 1) > good.txt
Code: Select all
> one.txt (echo 1)
> two.txt echo 1
If you took
Code: Select all
echo 1 > foo.bar
Code: Select all
> foo.bar echo 1
aGerman, so there's no misunderstanding, I'm neither disagreeing nor complaining with anything you said. These are merely illustrations for clarity.
When testing variations of these examples be alert that, as pointed out earlier by aGerman, the consecutive sequence 1> has a different interpretation.
taripo, my sequential comprehension process is exactly in line with your description - you articulated it well - and accordingly I have no intuitive flow with the other sequence; but I'm going to try to be open to the possibility that my habit might sometimes conflict with best practice.