Shell tips and tricks

Steven W. Orr steveo at syslang.net
Thu Nov 1 11:37:51 EDT 2007


On Wednesday, Oct 31st 2007 at 23:26 -0000, quoth Ben Scott:

=>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"?

FYI, bash does not have here-strings. perl does. python has something 
similar. 

=>
=>>  (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).

Speaking of envvars and variable interpolation, here's something to add 
to your .bash_profile

PS4='+${0##*/} line $LINENO: '

Try it out :-)

-- 
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net


More information about the gnhlug-discuss mailing list