How (or why) does this bash script snippet work?

Dean S. Messing deanm at sharplabs.com
Fri Jan 13 02:42:16 UTC 2012


On 12 Jan 2012 14:13:53, Anthony R Fletcher wrote:
> On 12 Jan 2012 at 14:02:50, Dean S. Messing wrote:
> > 
> > In a system script I find this snippet
> > 
> >   nodevs=$(< /proc/filesystems fgrep rootfs)
> > 
> > and don't understand the syntax.  According to the bash man page,
> > 
> > $(< file) is shorthand for $(cat file).  But then the above should read
> > 
> >   nodevs=$(< /proc/filesystems | fgrep rootfs)
> > 
> > However, the latter leaves $nodevs empty whereas the former puts the
> > stuff that fgrep processes into nodevs as it should.
> 
> 
> The command 
>  < /proc/filesystems fgrep rootfs
> 
> redirects the input from the file /proc/filesystems into the command
> "fgrep rootfs". You could rewrite this as
>   fgrep rootfs < /proc/filesystems
> 
> nodevs=$( command ) takes the result of the command inside the
> parentheses and assigns into the variable nodevs.

Ahh! A perfect explanation.   Thanks.

I did not realise (or, rather, think about) trying the entire command w/in $(...) on
the commandline.  The manpage says $(< file) is equivalent
to $(cat file), so I made the mathematically naive assumption that 
"< file" is equivalent to "cat file".

When "< file" didn't act like "cat file" on the commandline (which
I did try) I became confused.

Dean


More information about the users mailing list