Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a new entry to the PATH variable in ZSH

Tags:

linux

zsh

ubuntu

I'm using zsh and I'm trying to add a new entry (/home/david/pear/bin) to the PATH variable but I don't know how.

The thing that confuses me the most is that there's not a single reference to a PATH variable in my ~/.zshrc file, but doing echo $PATH returns:

/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games 

So a PATH variable is being set somewhere.

like image 907
David Barreto Avatar asked Jul 17 '12 20:07

David Barreto


People also ask

How do I add to PATH in ZSH?

To make the path variable persistent changes, we can use the shell configuration files loaded at each shell startup. In Bash, you can add the $PATH variable in the ~/. bashrc file. If you are on ZSH, you can add the $PATH in the ~/.

How do I add to my PATH variable?

To add a path to the PATH environment variableIn the System dialog box, click Advanced system settings. On the Advanced tab of the System Properties dialog box, click Environment Variables. In the System Variables box of the Environment Variables dialog box, scroll to Path and select it.


2 Answers

Actually, using ZSH allows you to use special mapping of environment variables. So you can simply do:

# append path+=('/home/david/pear/bin') # or prepend path=('/home/david/pear/bin' $path) # export to sub-processes (make it inherited by child processes) export PATH 

For me that's a very neat feature which can be propagated to other variables. Example:

typeset -T LD_LIBRARY_PATH ld_library_path : 
like image 63
ony Avatar answered Oct 02 '22 05:10

ony


Here, add this line to .zshrc:

export PATH=/home/david/pear/bin:$PATH 

EDIT: This does work, but ony's answer below is better, as it takes advantage of the structured interface ZSH provides for variables like $PATH. This approach is standard for bash, but as far as I know, there is no reason to use it when ZSH provides better alternatives.

like image 29
Linuxios Avatar answered Oct 02 '22 07:10

Linuxios