2

When I'm in one directory and echo path

pwd
/Users/me/src/some_proj

echo $PATH
/Users/me/.gem/ruby/2.0.0/bin:/opt/boxen/chruby/versions/2.0.0-
p353/lib/ruby/gems/2.0.0/bin:/opt/boxen/chruby/versions/2.0.0-
p353/bin:bin:/opt/boxen/chruby/bin:/opt/boxen/ruby-
build/bin:node_modules/.bin:/opt/boxen/nodenv/shims:/opt/boxen/nodenv/bin:
/opt/boxen/bin:/opt/boxen/homebrew/bin:/opt/boxen/homebrew/sbin:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin

When I'm in a different directory and echo path

pwd
/Users/me/src_personal/my_gems/som_gem

echo $PATH
bin:/opt/boxen/chruby/bin:/opt/boxen/ruby-build/bin:
node_modules/.bin:/opt/boxen/nodenv/shims:/opt/boxen/nodenv/bin:
/opt/boxen/bin:/opt/boxen/homebrew/bin:/opt/boxen/homebrew/sbin:
bin:/opt/boxen/chruby/bin:/opt/boxen/ruby-build/bin:
node_modules/.bin:/opt/boxen/nodenv/shims:/opt/boxen/nodenv/bin:
/opt/boxen/bin:/opt/boxen/homebrew/bin:/opt/boxen/homebrew/sbin:
bin:/opt/boxen/chruby/bin:/opt/boxen/ruby-build/bin:
node_modules/.bin:/opt/boxen/nodenv/shims:/opt/boxen/nodenv/bin:
/opt/boxen/bin:/opt/boxen/homebrew/bin:/opt/boxen/homebrew/sbin:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin

I get a different $PATH. I thought $PATH was a global variable in the shell that doesn't change. Why are these paths different in different directories?

2
  • 3
    It wouldn't changed automagically. Chances are that you executed a command/script inadvertently. – devnull May 6 '14 at 6:02
  • 1
    Does this change happen in a single logon session, under the same instance of the shell? Eg is the only difference between the two cases that the directory was changed using "cd"? If so, is the cd command aliased to something that could affect the PATH? – Johan May 6 '14 at 9:54
3

I know this is an ancient question, but we recently had some new ruby users hit this "apparently impossible" behavior. I'm answering in order to help others who have hit this.

It is because you are using ruby - which is applying local bundle and gem settings for each directory. Your PATH variable is changing simply due to a cd command - because ruby is handling the cwd change and applying the correct settings for that target directory (or global settings if there are no configurations in cwd) to the ENV.

A very common scenario is that a ruby user will run the bundle command, which is clearly on the PATH (try which bundle), change directories and then have the bundle command be unknown (no longer on the PATH because the PATH changed as a side effect of the cd command).

usually this can be remedied with gem install in the problematic directory.

1

There are no global variables in the shell, i.e. variables shared between different instances (processes). Exported variables are copied to child processes but from there, they are independent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.