0

I want to add my current working directory with its project-specific bin directories to my $PATH when the directories are present.

So I added these lines to my .zshrc:

[ -d "$(pwd)/vendor/bin" ] && export PATH="$(pwd)/vendor/bin":"$PATH"
[ -d "$(pwd)/node_modules/.bin" ] && export PATH="$(pwd)/node_modules/.bin":"$PATH"

The upper paths are prepended when I manually paste the commands to the terminal but otherwise they aren't. The $PATH is the same as before. I also tried different ways to write it like "${PATH}" and the zsh path function. They all do work when pasted manually but not when inside the .zshrc file.

--

I'm on macOs High Sierra v10.13.6 with the default Terminal and using zsh v5.7.1.

6
  • 2
    Your rc file is not executed from the directory you want it to. It's also bad practice to add pwd to your PATH. Why not just specify the full path you desire in your PATH?
    – jesse_b
    Jun 6, 2019 at 15:40
  • Well I thought pwd prints the full current path? I have many different projects and I don't want to paste them all manually. So whenever the directories are there I'd like to have them in my PATH. Why would that be bad practice?
    – dweipert
    Jun 6, 2019 at 15:44
  • 1
    Sure but your rc file isn't sourced every time you change directories, only once per shell session. The pwd at that time is likely your $HOME directory. It's likely not as big of an issue when you specify subdirectories after it but you don't want pwd in your PATH because you don't want to accidentally run programs that may be in the current directory that share the names of other programs in your bin directories. Also as you have found it simply doesn't work the way you want it to.
    – jesse_b
    Jun 6, 2019 at 15:48
  • Could you make "doesn't work" more explicit? Does it execute with errors? Does it not (appear) execute at all?
    – Jeff Schaller
    Jun 6, 2019 at 15:50
  • 1
    Just use a path relative to your $HOME rather than $(pwd).
    – Kusalananda
    Jun 6, 2019 at 16:08

3 Answers 3

1

With zsh specifically, you could do:

path[1,0]=( ~/projects/{vendor/,node_modules/.}bin(/N) )

Where:

path[1,0]=( list )

inserts the list at the start of the array ($path in zsh is an array tied to $PATH like in csh).

and (N/) is a glob qualifier where N enables nullglob for the glob and / selects files of type directory, so it add those two files ahead of $PATH provide they exist and are of type directory.

pwd is the command that prints the current working directory (the contents of the $PWD variable), it would only make sense to use $(pwd), or rather $PWD in your .zshrc if you wanted to add directories to $PATH conditionally to where zsh is started from. But even then, that would be dangerous, as for instance, if you started zsh from /tmp or someone else's directory, someone could have planted malicious files there that override standard commands such as ls or rm.

0

i don't think its possible to do it as you want, not with .rc (.rc is executed only once, it will not be executed later unless you specifically do it). But what about two aliases?

backupPATH=$PATH
function mkpath {
    [ -d "$(pwd)/vendor/bin" ] && export PATH="$(pwd)/vendor/bin":"$PATH"
    [ -d "$(pwd)/node_modules/.bin" ] && export PATH="$(pwd)/node_modules/.bin":"$PATH"
}
alias resetpath="PATH=$backupPATH"

With that you can add your current directory to the $PATH at any time.

0

As others have pointed out, your code does not necessarily work as you expect when you open a new terminal, as the current working directory may not be the one that contains your projects, and that piece of code is only run when a new shell starts.

Instead, you presumably know where your projects are, possibly in relation to your $HOME directory. If so, you may use that instead:

for binpath in vendor/bin node_modules/.bin; do
    if [ -d "$HOME/projects/$binpath" ]; then
        PATH=$HOME/projects/$binpath:$PATH
    fi
done

export PATH

Here, I'm assuming that $HOME/projects is where your projects are.

The export at the end is probably not needed as PATH is likely exported already.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .