1

In this article, I came across the following command:

< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-32}

I haven't seen this kind of expression in Bash yet. Specifically, the < at the start and the concatenation of /dev/urandom and tr surprised me. As far as I interpret this command, we're reading data from /dev/urandom into the tr command, but why is the < /dev/urandom first?

1 Answers1

2

Redirection operators can be placed anywhere in a simple command (see What are the shell's control and redirection operators? — “simple” is in the POSIX sense here, as opposed to compound).

Placing the input redirection first helps make the data flow clearer: reading from left to right, data is read from /dev/urandom, processed by tr, then piped to head.

Reading

tr -dc _A-Z-a-z-0-9 < /dev/urandom | head -c${1:-32}

instead means not knowing where tr gets its data from until after its arguments; there aren’t many here, but with more complex commands it can make quite a difference.

Stephen Kitt
  • 434,908
  • This explanation seems logical, but I regard the redirection operators as arrows explicitly depicting the data flow. If we put it first, it looks like the data flows "out" of the command, which confused me, while when we put it after the tr, we can directly see, the random numbers flow into tr. – Green绿色 Oct 13 '22 at 03:42