[NCLUG] shell scripting

S. Luke Jones luke at frii.com
Thu Sep 13 20:56:15 MDT 2001


Michael Dwyer wrote:
> Can you give a quick lesson on the $( ) construct, though?  I tried
> sort $(ls) and ... uh... it looks like it tried to sort the binary.

when you do $(cat /etc/termcap | grep foobar | sort | uniq) it's
exactly the same as `cat /etc/termcap | grep foobar | sort | uniq`
except that it isn't deprecated like backticks are. On the other
hand, the sh syntax file for bash highlighted $(whatever) properly
until 5.5 and since then makes a botch of it. Powerful argument for
backwards compatibility.

Actually the reason I detest `backticks` is that they look so much
like 'single-ticks', or at least when the font designer didn't know
how important they are, which is surprisingly often. The X courier
font gets it right but Lucida Typewriter makes the difference very
difficult to see. O'Reilly books (say it ain't so Tim) are set in
a face that isn't as clear as it could be.

Okay. Another reason I don't like backticks and the $(modern) equivalent
is that I tend to think from left to right. At heart, I'm a procedural
sort of guy where you youngsters are all functional recursion lovers.
I tend to solve problems like today's by doing

	find / ! -type d -mtime -1 -print0 | xargs -0 some-command

instead of

	some-command $(find / ! -type d -mtime -1)

even though the latter lets you run a different program to get
the find starting point or its command line arguments:

	some-command $(find / ! -type d -mtime -$(ls /etc/foo | wc -l))

or whatever crazed thing you might want to do.

There's a related reason (perhaps why my brain got canalized this
particular way) and that's because, back in the war, when we were
using backticks to save ink for propaganda posters, it was common
to have problems with programs that distinguished between '\n' and
' ' as filename separators on the command line, that is,

	some-command `echo *`

worked but

	some-command `ls *`

wouldn't. It wasn't worth the effort to remember which ones could
handle newlines so I just avoided the whole problem.

-- 
Luke Jones = luke/vortex/frii/fullstop/com



More information about the NCLUG mailing list