David Barreto
David Barreto

Reputation: 9497

Adding a new entry to the PATH variable in ZSH

I'm using zsh terminal, and I'm trying to add a new entry (/home/david/pear/bin) to the PATH variable. I don't see a reference to the 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 I know that the path variable is being set somewhere. Where is the PATH variable set / modified for the zsh terminal?

Upvotes: 524

Views: 829743

Answers (14)

ony
ony

Reputation: 13223

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)
# and don't forget to export to 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

This is what man zshbuiltins reports about -T.

-T [ scalar[=value] array[=(value ...)] [ sep ] ]
    This flag has a different meaning when used with -f; see below.  Otherwise
    the -T option requires zero, two,  or  three  argu ments  to  be  present.
    With no arguments, the list of parameters created in this fashion is shown.
    With two or three arguments, the first two are the name of a scalar and of
    an array parameter (in that order) that will be tied together in the manner
    of $PATH and $path. The optional third argument is a single-character
    separator which will be used to join  the  elements of the array to form
    the scalar; if absent, a colon is used, as with $PATH. Only the first
    character of the separator is significant;
    any remaining characters are ignored.
    Multibyte characters are not yet supported.

Upvotes: 647

Eduardo Lucio
Eduardo Lucio

Reputation: 2447

To try to help I will show you how to do the process using a BASH script.

IMPORTANT: The "PATH" environment variable must receive in its configuration a folder (eg: "/home/<YOUR_USER>/.scripts/zsh") and not a binary or script directly (eg: "/home/<YOUR_USER>/.scripts/zsh/my_bash_script").


Install a BASH script as an integrated terminal command

Make the BASH script executable

Run the following command to make "my_bash_script" script executable...

chmod +x /home/<YOUR_USER>/.scripts/zsh/my_bash_script

Add the BASH script to the PATH

Run the following command to temporarily add the BASH script directory to your "PATH"...

export PATH=$PATH:/home/<YOUR_USER>/.scripts/zsh  # Logged in as your user!

Add this line to the end of your ".zshrc" file so that it's automatically added each time you start a terminal session...

echo 'export PATH=$PATH:/home/<YOUR_USER>/.scripts/zsh' >> ~/.zshrc

Reload the ".zshrc" File

To apply the changes immediately, run...

source ~/.zshrc  # Logged in as your user!

Set Permissions

Next, you'll need to ensure that the BASH script have the correct permissions...

chown -R <YOUR_USER>:<YOUR_USER> /home/<YOUR_USER>/.scripts/zsh
chmod -R 700 /home/<YOUR_USER>/.scripts/zsh

You're Done! 😎

Upvotes: 1

Enes EREN
Enes EREN

Reputation: 7

You can add new file to /etc/paths.d and you can add your path that file. Don't forget to restart your terminal.

**#create new file to /etc/paths.d**
  touch /etc/paths.d/[filename]
**#open created file and add your path**
  vi /etc/paths.d/[filename]
**#save and close file and restart terminal**

Upvotes: -1

OctaviaLo
OctaviaLo

Reputation: 1396

The lower case path variable didn't work for me. When I opened up my .zshrc there was already an EXPORT PATH= "$HOME/.local/bin:$PATH", where $PATH is the current path variable set on the machine. All I did was append to the string adding a colon in front of the path. e.g. export PATH="$HOME/.local/bin:$PATH:$HOME/go/bin"`

Anything after the : are the new paths to be appended.

Upvotes: 0

RicHincapie
RicHincapie

Reputation: 3973

A native zsh way is to identify your install directory and create a file from where you will load your PATH modifications:

touch $ZSH/custom/usrenv.zsh

And add the new PATH directories like this inside that usrenv.zsh:

export PATH=$PATH:/home/myself/.foo/bin:/usr/local/bar/bin

The custom directory files *.zsh are sourced by default by the .oh-my-zsh.sh init script, as this snippet taken from it shows:

if [[ -z "$ZSH_CUSTOM" ]]; then
    ZSH_CUSTOM="$ZSH/custom"
fi
...
for config_file ("$ZSH_CUSTOM"/*.zsh(N)); do
  source "$config_file"
done
unset config_file

Upvotes: 1

jaycer
jaycer

Reputation: 3111

I'm on Monterey 12.4 and the only way I could change the path was using the helper function. Editing text files in nano did diddly squat

# append
path+=('/foo/bar/yourpath')
# export to sub-processes 
export PATH

Upvotes: 3

Linuxios
Linuxios

Reputation: 35783

Here, add this line to .zshrc:

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

EDIT: This does work, but ony's answer above 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.

Upvotes: 502

MNassar
MNassar

Reputation: 377

If you are on macOS (I'm on Monterey 12.3.1), you may have been pulling your hair like I did metaphorically. These instructions above all worked for me within the terminal session, but I could never get it to persist no matter what I did with export. Moreover, I couldn't find the .zshrc anywhere.

Turns out Apple does it differently. The file you need to edit is etc/paths. You can simply sudo nano /etc/paths and add your path in a new line. Then simply restart terminal and voila.

Upvotes: 10

Micah Elliott
Micah Elliott

Reputation: 10264

You can append to your PATH in a minimal fashion. No need for parentheses unless you're appending more than one element. It also usually doesn't need quotes. So the simple, short way to append is:

path+=/some/new/bin/dir

This lower-case syntax is using path as an array, yet also affects its upper-case partner equivalent, PATH (to which it is "bound" via typeset).

(Notice that no : is needed/wanted as a separator.)

Common interactive usage

Then the common pattern for testing a new script/executable becomes:

path+=$PWD/.
# or
path+=$PWD/bin

Common config usage

You can sprinkle path settings around your .zshrc (as above) and it will naturally lead to the earlier listed settings taking precedence (though you may occasionally still want to use the "prepend" form path=(/some/new/bin/dir $path)).

Related tidbits

Treating path this way (as an array) also means: no need to do a rehash to get the newly pathed commands to be found.

Also take a look at vared path as a dynamic way to edit path (and other things).

You may only be interested in path for this question, but since we're talking about exports and arrays, note that arrays generally cannot be exported.

You can even prevent PATH from taking on duplicate entries (refer to this and this):

typeset -U path

PATH pre-populated

The reason your path already has some entries in it is due to your system shell files setting path for you. This is covered in a couple other posts:

Upvotes: 120

nicolas
nicolas

Reputation: 9805

to verify your new directory has been added correctly, you can use

print -l $path

thanks to the fact that its type is known to be an array

Upvotes: -1

Aurora
Aurora

Reputation: 133

for me PATH=$PATH:/path/to/file/bin then export PATH worked. to check echo $PATH . other solutions are adding the path temporarily.

Upvotes: 2

saneryee
saneryee

Reputation: 3405

  1. Added path to ~/.zshrc

    sudo vi ~/.zshrc

    add new path

    export PATH="$PATH:[NEW_DIRECTORY]/bin"
    
  2. Update ~/.zshrc

    Save ~/.zshrc

    source ~/.zshrc

  3. Check PATH

    echo $PATH

Upvotes: 41

Dimitar
Dimitar

Reputation: 1928

OPTION 1: Add this line to ~/.zshrc:

export "PATH=$HOME/pear/bin:$PATH"

After that you need to run source ~/.zshrc in order your changes to take affect OR close this window and open a new one

OPTION 2: execute it inside the terminal console to add this path only to the current terminal window session. When you close the window/session, it will be lost.

Upvotes: 26

Siva Praveen
Siva Praveen

Reputation: 2333

one liner, without opening ~/.zshrc file

echo -n 'export PATH=~/bin:$PATH' >> ~/.zshrc

or

echo -n 'export PATH=$HOME/bin:$PATH' >> ~/.zshrc

To see the effect, do source ~/.zshrc in the same tab or open a new tab

Upvotes: 74

Related Questions