Search code examples
linuxbashwc

> and < difference Bash


I have to test if pathname is a regular file and if it's length is greater 50 bytes , for this reason I do like this:

if [[ -f $path && `wc -c < $path` -gt 50 ]]; then ......

and it works , but , for curiosity , I tried to do also like this:

if [[ -f $path && `$path > wc -c` -gt 50 ]]; then ......

but it doesn't work and I don't understand why.

For this reason I ask you the difference between < and > operator in Bash.


Solution

  • These operators are not commutative (position aren't swappable).

    wc -c < $path means launch wc and use the file at $path as the input.

    $path > wc -c means launch the executable at $path (which in your case $path isn't an executable) and send it's output to the file at wc.

    As you can see the second one doesn't really make sense. Always make the executable the first operand (argument), and the file you are reading from or writing to the second operand.