[SVN] Problems with usage of «$(type -p "${TM_RUBY:-ruby}")» in commands

Martin Kühl martin.kuehl at gmail.com
Tue Oct 13 14:01:08 UTC 2009


I recently ran into problems running scripts from TM and traced the cause to
having defined `ruby` as a shell function that launched `irb` when called
without arguments. The investigation showed a number of uses of the
code snippet `$(type -p "${TM_RUBY:-ruby}")`, particularly in the
following idiom:

    export TM_RUBY=$(type -p "${TM_RUBY:-ruby}")

I think this idiom is problematic for two reasons, both of which can be fixed:

1. The launch path of `ruby` can contain spaces which the above expression
   doesn’t mask, resulting in erroneous values in the variable but no visible
   errors. Simple demonstration:

    $ export VAR=foo bar # Note that the additional `bar` is just ignored
    $ echo $VAR
    foo

  This problem can be fixed by quoting the subshell expression, like this:

   export TM_RUBY="$(type -p "${TM_RUBY:-ruby}")"

  (Yes, the parser works well enough not to choke on that kind of nesting.)

2. The simple `type -p` command will print the launch path _only_ when its
   argument would in fact be invoked from that path as a command. What this
   means is that when one defines a shell function called `ruby` (like I had),
   `type -p ruby` will generate the empty string. Demo:

    $ type -p ruby
    /usr/bin/ruby
    $ function ruby () { :; }
    $ type -p ruby

   This problem can be worked around by using which(1) instead of `type -p` to
   find the launch path. In my experience, a lot of wide-spread shell
   configurations actually make `which` an alias, which is why I commonly
   advise people to explicitly call which(1) in scripts. This would look like:

    export TM_RUBY="$(command which "${TM_RUBY:-ruby}")"

   or:

    export TM_RUBY="$(/usr/bin/which "${TM_RUBY:-ruby}")"

I’m also not sure why the ruby launch path is used in the commands at all, but
if, as it seems, that’s the accepted way, I hope these fixes will be
considered.

Thanks,
Martin



More information about the textmate-dev mailing list