Search code examples
shellzsh

shell fucntion cd breaks $PATH


I'm trying to use script to change working directory.

in .zshrc:

source myscript

in myscript:

fucntion cdd(){
    $path=(find xxx |fzf) #just a command to get a path
    cd $path
}
zle -N cdd
bindkey '\et' cdd #Alt+t

I want to do something like fzf doing.

After I pressed alt+t ,I did changed the working directory, but it seemes that my environment is broken.(I need to Enter to get shell promote back)

              (<---note I need to press Enter to get shell promote)
➜  c vim
zsh: command not found: vim
➜  c ls
zsh: command not found: ls
➜  c pwd
/home/xxx/document/c
➜  c echo $PATH
./document/c
➜  c

The last command pwd works well. It's strange that my $PATH changed. I can't figure out why.


Solution

  • $path=(find xxx |fzf) is wrong. You want:

    mypath=$(find ...)
    

    That is, omit the leading $. In general, path is a bad name for a variable, so I've use mypath. PATH has special meaning in all shells, and path is significant in zsh. In particular, path is an array variable that consists of directories to be searched for executables. path and PATH are tied (see typedef -T in the zsh documentation), so modifying path effectively changes PATH.