Shell tips and tricks

Ben Scott dragonhawk at gmail.com
Wed Oct 31 23:26:48 EDT 2007


On 10/31/07, VirginSnow at vfemail.net <VirginSnow at vfemail.net> wrote:
>  (3) Here-documents and here-strings.  How is this here-document going
>      to be munged?  (i.e., What the heck is going to be substituted
>      in my hear-document?)

  "Hear-document" is a typo with humor potential.  (Implementation is
left as an exercise for the reader.)

  For here-documents, I use a mnemonic: If the delimiter word is
quoted or escaped, the entire here-doc is also "escaped" with it --
i.e., no expansion, substitution, or other magic will happen.   If the
delimiter word is just left bare (like an unquoted, unescaped command
line), then most of the normal substitutions/expansions that happen
can be done (just like in said command line).

  If you put a dash before the delimiter word, then you can indent
your hear-doc to line up with everything else.  Mnemonic: A dash is a
small line, and you're lining things up.

  Here-strings I never saw the point of.  Aren't they just like "echo
foo | command"?

>  (4) All that ${%%:-//} funny variable parsing stuff.  (Good
>      replacement for some calls to cut/sed.)

  The syntax for those is painful, but can be very useful.  It's still
one of those things I have to look up every time I use it.

  Here's a useful idiom in this vein, which I picked up relatively recently:

	EDITOR="${EDITOR:-/bin/vi}"

  "Set EDITOR to vi(1), unless EDITOR is already set."  This is useful
to put at the top of shell scripts that will invoke an editor.  So if
the user specifies their editor, that gets honored; otherwise, it
defaults to vi(1).

-- Ben


More information about the gnhlug-discuss mailing list