From 6cdaefd08f97a3e918ea99daa1b4246571334c9e Mon Sep 17 00:00:00 2001 From: Artem Pyanykh Date: Sat, 8 Feb 2014 00:39:04 +0400 Subject: [PATCH] Fix chruby-exec sourcing chruby.sh chruby-exec works as follows: 1. test for presence of chruby with `command -v chruby >/dev/null` 2. source `chruby.sh` if chruby is not loaded 3. execute `chruby $ruby && $command` in a subshell The problem is that sourced functions, unlike environmental variables, are not 'inherited' by subshells. (It is true at least in Ubuntu 12.04 LTS, where I tested this) With "basic setup" you copy `chruby.sh` to `/etc/profile.d`, and this `chruby.sh` sources `/usr/share/local/chruby/chruby.sh` Then, when you invoke `chruby_exec` it works just fine, because `/usr/share/local/chruby/chruby.sh` is sourced automaticaly by bash through `/etc/profile` initialization script (which sources `/etc/profile.d/chruby.sh`) It would be great if you consider moving the process of sourcing `/usr/share/local/chruby/chruby.sh` from the top of `chruby_exec` to a command executed by a subshell. It would allow to use `chruby_exec` without needing to source `/usr/local/chruby/chruby.sh` with shell init scripts (`/etc/profile.d/chruby.sh`) About function inheritance in a subshells You could check it with the following setup: # set_func.sh: function test_func() { 0 } # file get_func.sh: source ./set_func.sh type test_func | head -1 exec "$SHELL" -i -l -c "type test_func | head -1" and then: $ ./get_func.sh test_func is a function bash: type: test_func: not found --- bin/chruby-exec | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/chruby-exec b/bin/chruby-exec index 511e8a7e..3eb6c09f 100755 --- a/bin/chruby-exec +++ b/bin/chruby-exec @@ -1,7 +1,5 @@ #!/usr/bin/env bash -source "${0%/*}/../share/chruby/chruby.sh" - case "$1" in -h|--help) echo "usage: chruby-exec RUBY [RUBYOPTS] -- COMMAND [ARGS...]" @@ -33,7 +31,9 @@ if (( $# == 0 )); then exit 1 fi -command="chruby $(printf "%q " "${argv[@]}") && $(printf "%q " "$@")" +chruby_sh="${0%/*}/../share/chruby/chruby.sh" +source_command="command -v chruby >/dev/null || source $chruby_sh" +command="$source_command; chruby $(printf "%q " "${argv[@]}") && $(printf "%q " "$@")" if [[ -t 0 ]]; then exec "$SHELL" -i -l -c "$command" else exec "$SHELL" -l -c "$command"