cedricdlb
cedricdlb

Reputation: 759

cd into a specific directory changes PATH variable

When I change directory (cd) into one specific directory (which contains a rails application) it changes the PATH variable, removing many directories from PATH including /bin, /usr/bin, and /usr/local/bin, after which I get many bash errors of "command not found" for commands like which, ls, grep, etc. It appears to be only that one directory which causes the problem, when I cd into it or cd from it. I don't see any obvious files in the directory which would be changing the PATH variable in this way. Before I enter the directory, my PATH looks like this:

$ echo $PATH
/usr/local/opt/node@10/bin:/usr/local/opt/[email protected]/bin:/Users/cedric/.rbenv/shims:/Users/cedric/.rbenv/shims:/Users/cedric/.rvm/gems/ruby-2.3.7/bin:/Users/cedric/.rvm/gems/ruby-2.3.7@global/bin:/Users/cedric/.rvm/rubies/ruby-2.3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/cedric/.rvm/bin:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin:/usr/local/bin/AWS_Tools/AutoScaling-1.0.61.6/bin:/usr/local/bin/AWS_Tools/CloudWatch-1.0.20.0/bin:/usr/local/bin/AWS_Tools/AWS-ElasticBeanstalk-CLI-2.6.3/api/:/usr/local/bin/AWS_Tools/AWS-ElasticBeanstalk-CLI-2.6.3/AWSDevTools/:/usr/local/bin/AWS_Tools/AWS-ElasticBeanstalk-CLI-2.6.3/eb/:/usr/local/bin/AWS_Tools/AWS-ElasticBeanstalk-CLI-2.6.3/eb/linux/python2.7/:/usr/local/bin/AWS_Tools/AWS-ElasticBeanstalk-CLI-2.6.3/eb/macosx/python2.7/:/usr/local/bin/AWS_Tools/AWS-ElasticBeanstalk-CLI-2.6.3/eb/macosx/python2.7/eb:/usr/local/mysql/bin:/Users/cedric/.rbenv/bin:/Users/cedric/.rbenv/shims:/usr/local/mysql/bin

After I enter the directory, my PATH looks like this:

cedric@Computer togo [develop] $ cd .
sed: illegal option -- r
usage: sed script [-Ealn] [-i extension] [file ...]
       sed [-Ealn] [-i extension] [-e script] ... [-f script_file] ... [file ...]
-bash: cat: command not found
-bash: cat: command not found
-bash: cut: command not found
cedric@Computer togo $ echo $PATH
/Users/cedric/.rvm/gems/ruby-2.3.7@togo/bin:/Users/cedric/.rvm/gems/ruby-2.3.7@global/bin:/Users/cedric/.rvm/rubies/ruby-2.3.7/bin:/Users/cedric/.rvm/bin:
-bash: cut: command not found
cedric@Computer togo $ 

This wasn't a problem before today. Any thoughts on what could be causing this?

Thank you

RESPONSES TO COMMENTS:

$ echo "$PROMPT_COMMAND"
update_terminal_cwd;

$ type update_terminal_cwd
update_terminal_cwd is a function
update_terminal_cwd () 
{ 
    local SEARCH=' ';
    local REPLACE='%20';
    local PWD_URL="file://$HOSTNAME${PWD//$SEARCH/$REPLACE}";
    printf '\e]7;%s\a' "$PWD_URL"
}

(seems like that's just replacing "%20" for " " whatever PWD_URL is.)

$ type cd returns

cd is a function
cd () 
{ 
    __zsh_like_cd cd "$@"
}

and

$ type __zsh_like_cd
__zsh_like_cd is a function
__zsh_like_cd () 
{ 
    \typeset __zsh_like_cd_hook;
    if builtin "$@"; then
        for __zsh_like_cd_hook in chpwd "${chpwd_functions[@]}";
        do
            if \typeset -f "$__zsh_like_cd_hook" > /dev/null 2>&1; then
                "$__zsh_like_cd_hook" || break;
            fi;
        done;
        true;
    else
        return $?;
    fi
}

In the output of set I find:

chpwd_functions=([0]="__rvm_cd_functions_set")
...
__rvm_after_cd () 
{ 
    \typeset rvm_hook;
    rvm_hook="after_cd";
    if [[ -n "${rvm_scripts_path:-}" || -n "${rvm_path:-}" ]]; then
        source "${rvm_scripts_path:-$rvm_path/scripts}/hook";
    fi
}
...
__rvm_cd_functions_set () 
{ 
    __rvm_do_with_env_before;
    if [[ -n "${rvm_current_rvmrc:-""}" && "$OLDPWD" == "$PWD" ]]; then
        rvm_current_rvmrc="";
    fi;
    __rvm_project_rvmrc 1>&2 || true;
    __rvm_after_cd || true;
    __rvm_do_with_env_after;
    return 0
}

There was a lot of output for set | grep sed (48 lines) so I searched for such output with PATH:

 $ set | grep sed | grep -i path
            \command \cat "$environment_file_path" | __rvm_grep -Eo "[^ ]+=[^;]+" | __rvm_sed -e 's/\$PATH/'"${PATH//\//\\/}"'/' -e 's/\${PATH}/'"${PATH//\//\\/}"'/';
            rvm_silence_logging=1 "$rvm_scripts_path/gemsets" list strings | __rvm_sed "s/ (default)//; s/^/$current_ruby${rvm_gemset_separator:-@}/ ; s/@default// ;"
    __rvm_sed -e 's/#.*$//g' -e 's#\[##g' -e 's#\]##g' < "$rvm_path/config/known" | sort -r | uniq;
         <log>Usually this is caused by shell initialization files. Search for <code>PATH=...</code> entries.
            PATH="$(\sed -E -e 's#/+#/#g' -e 's#/:#:#' <<<$PATH)";
            PATH="$(\sed -r -e 's#/+#/#g' -e 's#/:#:#' <<<$PATH)";
                __rvm_sed_i "$__rvmrc_warning_path" -e "\#^${1}\$# d" -e '/^$/ d';
                __rvm_sed_i "$__rvmrc_warning_path" -e "\#/${__rvmrc_type}\$# d" -e '\#^$# d';
                __rvm_sed_i "$__rvmrc_warning_path" -e "\#^${1}\$# d" -e '\#^$# d';

Also, searching for places where PATH is set, it seems like they are all including the original value of PATH in the new value:

$ set | grep "\<PATH *="
PATH=/usr/local/opt/node@10/bin:/usr/local/opt/[email protected]/bin:/Users/cedric/.rbenv/shims:/Users/cedric/.rbenv/shims:/Users/cedric/.rvm/gems/ruby-2.3.7/bin:/Users/cedric/.rvm/gems/ruby-2.3.7@global/bin:/Users/cedric/.rvm/rubies/ruby-2.3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/cedric/.rvm/bin:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin:/usr/local/bin/AWS_Tools/AutoScaling-1.0.61.6/bin:/usr/local/bin/AWS_Tools/CloudWatch-1.0.20.0/bin:/usr/local/bin/AWS_Tools/AWS-ElasticBeanstalk-CLI-2.6.3/api/:/usr/local/bin/AWS_Tools/AWS-ElasticBeanstalk-CLI-2.6.3/AWSDevTools/:/usr/local/bin/AWS_Tools/AWS-ElasticBeanstalk-CLI-2.6.3/eb/:/usr/local/bin/AWS_Tools/AWS-ElasticBeanstalk-CLI-2.6.3/eb/linux/python2.7/:/usr/local/bin/AWS_Tools/AWS-ElasticBeanstalk-CLI-2.6.3/eb/macosx/python2.7/:/usr/local/bin/AWS_Tools/AWS-ElasticBeanstalk-CLI-2.6.3/eb/macosx/python2.7/eb:/usr/local/mysql/bin:/Users/cedric/.rbenv/bin:/Users/cedric/.rbenv/shims:/usr/local/mysql/bin
            PATH="$2:$PATH"
            PATH="$PATH:$2"
        PATH="${rvm_user_path_prefix}:$PATH";
            PATH="$PATH:${rvm_bin_path}";
            PATH="${rvm_bin_path}:$PATH";
    printf "%b" "export PATH=\"${__path}:\$PATH\"\n" > "$file_name";
    PATH="${_new_path[*]}:$PATH";
    env | __rvm_grep -E '^GEM_HOME=|^GEM_PATH=|^PATH=';
         <log>Usually this is caused by shell initialization files. Search for <code>PATH=...</code> entries.
                        PATH="${save_PATH}";
            PATH="$(\sed -E -e 's#/+#/#g' -e 's#/:#:#' <<<$PATH)";
            PATH="$(\sed -r -e 's#/+#/#g' -e 's#/:#:#' <<<$PATH)";
                    command_to_run=(/usr/bin/env PATH="${PATH}${missing_paths}" "${command_to_run[@]}");
        PATH="${_OLD_VIRTUAL_PATH}";
    PATH="${__path_prefix:-}${__path_prefix:+:}$__save_PATH${__path_suffix:+:}${__path_suffix:-}";

Does any of the above raise red flags as a possible culprit? Should I be checking for something more specific?

RESPONSE TO @Roadowl (in that directory):

 $ /bin/ls -la -- -*
ls: -*: No such file or directory

Upvotes: 2

Views: 493

Answers (1)

djechlin
djechlin

Reputation: 60748

chruby can do this, using preexec_function in zsh and trap in bash. My answer from https://github.com/postmodern/chruby/blob/master/share/chruby/auto.sh:

You're probably sourcing https://github.com/postmodern/chruby/blob/master/share/chruby/auto.sh

which includes a trap command in bash and a preexec_function command in zsh to run chruby_auto when you change directory.

What chruby_auto does is look for the .ruby_version file you have and set ruby from that file by changing path variables.

Upvotes: 1

Related Questions