Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I recently discovered this method for generating a 9 character password from the linux prompt in bash:

< /dev/urandom tr -dc A-Za-z0-9_ | head -c9

What is < doing?

share|improve this question
1  
I suggest changing subject of your posting, since the answer doesn't seem to be in direct relation with Subject line. :) –  poige Jun 15 '12 at 3:29
add comment

2 Answers 2

up vote 11 down vote accepted

In case you're wondering, this is just a less common usage of the input redirector. The shell takes the file specified as input and its default behavior is to echo it to stdout.

The breakdown of the command you have:

< /dev/urandom tr -dc A-Za-z0-9_ | head -c9

<: Input redirector. Shell echoes contents of file (/dev/urandom)

tr: from tr --help:

Translate, squeeze, and/or delete characters from standard input, writing to standard output.

The options -dc are equivalent to --delete --complement, which means to delete the complement of the following set of characters, i.e. all characters NOT in the set. That excludes non alphanumeric & underscore characters in your example.

head: print the first few lines of something (default 10). the -c9 switch tells it how many bytes to print instead of lines.

Put it all together and you get, "print me the first 9 alphanumeric or underscore bytes from /dev/urandom". :)

share|improve this answer
2  
It is "standard", in the sense that any POSIX sh should parse redirections out of anywhere in a simple command (even in the middle). It's just uncommon to not put them at the end. –  ormaaj Jun 15 '12 at 8:13
    
You know, I almost went back and changed that wording, so I went ahead and updated it now. You're right... I was basically just quoting the page in the link. It's more proper to say it's "less common" because as far as I know it's a very well-supported usage. –  user19866 Jun 15 '12 at 14:06
add comment

Try < /usr/include/stdio.h head — it should be file, dude.

In case you need a program output being given as file, it can be done this way: vim <(echo test)

share|improve this answer
    
Apologies for the unaccept, palintropos' answer is just too thorough –  Mike Pennington Jun 22 '12 at 16:14
    
@MikePennington, I don't think that's the more is better, but nevermind anyways. –  poige Jun 22 '12 at 16:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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