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

Dean S. Messing deanm at sharplabs.com
Fri Jan 13 02:47:08 UTC 2012


On 12 Jan 2012 at 20:34:26, Mark LaPierre wrote:
> On 01/12/2012 02:13 PM, 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.
> >
> > 			Anthony.
> >
> 
> or you could do:
> 
> nodevs=`fgrep rootfs /proc/filesystems`
> 
> The only problem is that putting the command inside ticks launches a 
> child process that consumes additional resources.

Correct.

But I lied slightly.  What was actually in the script was:

  nodevs=$(< /proc/filesystems awk ...)

where ... was complicated. I wanted to simply the issue so I subbed
something trivial for the command.

Dean



More information about the users mailing list