1

I have recently decided to move my personal scripts to a directory in my $HOME. I have included this directory in my $PATH variable but have had ZSH respond with "command not found" when attempting to run them. The following line is what I appended to .xprofile

PATH="$PATH:~/.local/share/";export PATH

I have a few scripts in ~/.local/share/ but haven't been able to execute them in ZSH, but in bash it works fine. I have tried adding the live above to my .zshrc, but that has not worked.

2
4

~ is not a variable and does not behave like a variable.

Shells generally don't expand ~ when it is quoted. You may use $HOME instead of ~ in any shell to make sure that you get the correct path to your home directory, without relying on the shell's special treatment of the tilde character (which is a shortcut mainly for use in interactive shells).

$HOME does behave like you'd expect a variable to behave, i.e. it gets expanded to the path of your home directory when it's quoted using double quotes.

Also note that it's unlikely that PATH is not already an environment variable. Exporting it is therefore not needed.

In the zsh shell, to add ~/.local/share to the end of your command search path, you could also do

path+=~/.local/share

or

path+=$HOME/.local/share

The array variable path is tied to the scalar variable PATH, meaning that when you update the array path as above, the added element gets added to the end of the value $PATH.

4
  • In POSIX shells and zsh, PATH=$PATH:~/.local/share would work. – Stéphane Chazelas May 30 '20 at 10:41
  • @StéphaneChazelas It would, but you're bound to get someone doing export PATH=... by reflex, so the quotes are possibly warranted. – Kusalananda May 30 '20 at 10:42
  • @StéphaneChazelas That's a good point. – Kusalananda May 30 '20 at 10:44
  • very informative, thank you. – Lara May 30 '20 at 12:08
0

ZSH startup scripts are ~/.zshrc and ~/.zshenv. I usually put global environment variables like PATH in ~/.zshenv.

It is possible that some other instructions in your startup scripts are overwriting/replacing your PATH. To see what is happening during zsh startup, run:

zsh -x 2>&1 </dev/null |grep PATH 

Startup scripts do not affect shells that are already running. To get those updated:

  • source the scripts e.g. . ~/.zshenv
  • re-exec zsh (exec zsh -l)
  • log out & log back in

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.