[mdlug] UNIX tips: Learn 10 good UNIX usage habits

Robert Meier eaglecoach at wwnet.com
Mon Mar 10 00:41:57 EDT 2008


Ray, Aaron,

>  ~ $ echo "${VAR}a"
>  tmp/*a
>  ~ $ echo ${VAR}a
>  tmp/a
>
> This all works the same for me, except that last one. On my system the
> last two do the same thing:
>
>  pts/1 $ echo "{$VAR}a"
>  {tmp/*}a
>  pts/1 $ echo {$VAR}a
>  {tmp/*}a
>  pts/1 $

> I don't know if the author used a different shell, or has different
> globbing options set, but I don't see just how that last one is even
> supposed to work.

The difference is the subtle difference between {$ and ${ .
The ${...} "quotes" the name inside which may have special variables, spaces,
...
{$ is treated as two separate tokens.
The { is just a character prepended to the word, and $... evaluates
the token that follows, which ends at the next space or special character.

  {$VAR}a is tokenized as '{'"$VAR"'}''a' .
  ${VAR}a is tokenized as "${VAR}"'a'



>[
> command || expression
> read as: Do command OR (if you couldn't) do expression
> command && expression
> read as: do command AND (if successful) continue with expression
>]

As Aaron's summary shows,
  || and && behave more like perl's
  unless(command) {expression} and if(command) {expression}
and less like true logical operators.



>> This article seems worth the read, but seems to need a little work.

> Yeah, it's not quite as clear as it should be....

I posted some suggestions to IBM.
I included also the conflict between xargs (hint 8)
which splits filenames with spaces and quoting (hint 4)
which maintains filenames.
I suggested using find ... | while read f; do ... "$f" ...; done
as more portable, flexible, and less error prone.

Thanks for the pointer,
-- 
Bob

  "Each problem that I solved became a rule,
   which served afterwards to solve other problems."
     -- Rene Descartes



More information about the mdlug mailing list