Skip to content

Instantly share code, notes, and snippets.

@nex3
Created March 16, 2018 00:45
Show Gist options
  • Save nex3/c395b2f8fd4b02068be37c961301caa7 to your computer and use it in GitHub Desktop.
Save nex3/c395b2f8fd4b02068be37c961301caa7 to your computer and use it in GitHub Desktop.

The PATH is an important concept when working on the command line. It's a list of directories that tell your operating system where to look for programs, so that you can just write script instead of /home/me/bin/script or C:\Users\Me\bin\script. But different operating systems have different ways to add a new directory to it:

Windows

  1. The first step depends which version of Windows you're using:
  • If you're using Windows 8 or 10, press the Windows key, then search for and select "System (Control Panel)".
  • If you're using Windows 7, right click the "Computer" icon on the desktop and click "Properties".
  1. Click "Advanced system settings".
  2. Click "Environment Variables".
  3. Under "System Variables", find the PATH variable, select it, and click "Edit". If there is no PATH variable, click "New".
  4. Add your directory to the beginning of the variable value followed by ; (a semicolon). For example, if the value was C:\Windows\System32, change it to C:\Users\Me\bin;C:\Windows\System32.
  5. Click "OK".
  6. Restart your terminal.

Mac OS X

  1. Open the .bash_profile file in your home directory (for example, /Users/your-user-name/.bash_profile) in a text editor.
  2. Add export PATH="your-dir:$PATH" to the last line of the file, where your-dir is the directory you want to add.
  3. Save the .bash_profile file.
  4. Restart your terminal.

Linux

  1. Open the .bashrc file in your home directory (for example, /home/your-user-name/.bashrc) in a text editor.
  2. Add export PATH="your-dir:$PATH" to the last line of the file, where your-dir is the directory you want to add.
  3. Save the .bashrc file.
  4. Restart your terminal.
Copy link

ghost commented Feb 12, 2020

** (gedit:23414): WARNING **: 12:31:52.288: Set document metadata failed: Setting attribute metadata::gedit-encoding not supported

am facing this error when trying to add a path

@FTheodore
Copy link

FTheodore commented Feb 12, 2020

use nano instead of gedit, gedit has issues when trying to run from tty

** (gedit:23414): WARNING **: 12:31:52.288: Set document metadata failed: Setting attribute metadata::gedit-encoding not supported

am facing this error when trying to add a path

@franciscotv
Copy link

small correction use PATH instead of $PATH in the first parameter after export:
export PATH="$HOME/appl/code/leap-demo-app:$PATH"

Copy link

ghost commented Feb 27, 2020 via email

@franciscotv
Copy link

my pleasure :)
think about why using one instead of the other.

  • assign
  • expand
    that is why, they are different.

@SimonScharf
Copy link

SimonScharf commented Jul 20, 2020

I tried following these instructions on my Mac, edited my .bash_profile and appended the line:
export PATH="/Users/.../dart-sass:$PATH" (where ... is the rest of the absolute path of the dart-sass directory)
However, when I run sass --version in terminal, I get the error -bash: sass: command not found.

Am I doing anything wrong? It is very frustrating and confusing.

Edit: Problem was fixed when I tried the line: export PATH="$PATH:/Users/.../dart-sass". I am still confused as to how this fixed the problem though.

@davidenoma
Copy link

for me: on .bashrc
I just added
PATH=$PATH:/directory

@kjcaputa
Copy link

You do not have to restart terminal. You can use source command
source ~/.bashrc

@franciscotv
Copy link

@kjcaputa
Thanks!, source command is awesome!
example

@Zorono
Copy link

Zorono commented Nov 15, 2020

Thank you, dude!!
in my case (Ubuntu Desktop 20.10) I just ran the following command in the Terminal PATH="/directory:$PATH"

@TMSantos
Copy link

TMSantos commented Jan 1, 2021

For those using Z-Shell (zsh) don't forget it's .zshrc instead of .bash_profile

@michee1367
Copy link

thanks

@rafaVls
Copy link

rafaVls commented Feb 10, 2021

For those using Z-Shell (zsh) don't forget it's .zshrc instead of .bash_profile

This worked for me using zsh, thanks!

@davidliu611
Copy link

why do we need to append ":$PATH" at the end?

@Ghasak
Copy link

Ghasak commented Apr 8, 2021

@davidliu611 This is a simple concatenation of the string in the bash scripting language. You allow the original PATH string to be concatenating with the dir that you specified. check this out using
echo $PATH (in terminal)
you will see that your dir has been added at the beginning of the PATH.
PATH is the location where the system will look for other apps, scripts, env. .etc.

@Screeeech
Copy link

worked great, thank you so much

Copy link

ghost commented Jun 25, 2021

It is not working for me. I am using kali linux. Do you know what is the problem?

@Midhlaj2006
Copy link

Life saver 👍

@JosephBerm
Copy link

Excellent work. Thank you for this upload.

@moghaazi
Copy link

moghaazi commented Sep 9, 2021

For those using Z-Shell (zsh) don't forget it's .zshrc instead of .bash_profile

I forget that and spent 3 hours trying all solutions, till I read your comment.

Copy link

ghost commented Oct 21, 2021

Works on Command prompt. But not in Bash? can anyone help?.

@sourabh-kumar-jha
Copy link

it still says unknown command lvim

@byteknight
Copy link

byteknight commented Feb 26, 2022

RapidEE is an outstanding alternative to the Windows built-in control panel dialog box for environment variable management and viewing, including the PATH variable, of course. For those who like text walls, read on! xP

BTW, thanks nex3. Cool tips!


TL;DR Version:

Although it's not necessary in the slightest, and for the standard non-hacker end-user probably smacks of overkill, if you are like me in meeting the following two criteria:

  • A. Are forced to use Windows due to its popularity or your own masochistic tendencies; and furthermore
  • B. Make use of environment variables (esp. PATH) fairly often, like most developers,

then you should find in RapidEE a beautiful tool to replace the tiny control panel dialog box. The native Windows method is fine for simple, "one-off" changes to PATH or a different variable, for a more comprehensive, "bird's eye" view of both system and user variables and the ability to add/delete or modify them—all quickly and easily, without the confusion the regular dialog can entail—the handy, small program called Rapid Environment Editor (or RapidEE) is more than sufficient. Although it's closed-source (afaik), it's free, up-to-date and even supports operations on variables via command-line arguments.

I'm fully aware there are other apps that fill the same need, many of them FOSS, but when I use Windows RapidEE has served me very well, and its features continue to save me many headaches from confusion, tedium and their ilk. Anyway, happy hacking, guys, and thanks again nex3!

@ricky900
Copy link

i tried for lunarvim and its not working
export PATH="$PATH: ~/.local/bin/"

@fatherofphysics
Copy link

i tried for lunarvim and its not working export PATH="$PATH: ~/.local/bin/"

use this export PATH="your-dir:$PATH"

@linconl-cmd
Copy link

In my experience , used : export PATH=/.cargo/bin:/.local/bin:$PATH In file .bashrc appended last line
ps. DIST. ARCH

@mauriciocoruja
Copy link

Still helpful. tks

@Chirpingdusty
Copy link

for those using fish on macOS,

  1. go to /Users/account-name/.config/fish

  2. open config.fish and paste the following line inside the file

export PATH="/Users/account-name/.local/bin:$PATH"

@billnice250
Copy link

billnice250 commented May 8, 2022

Sometimes, I noticed modifying the .bashrc file doesn't work instead,
Adding the new PATH in .zshrc file (which is the default shell for MacOs) works for me.
add a new line in the file $HOME/.zshrc:

#for example
export PATH="your-bin-dir:$PATH"

then run
source $HOME/.zshrc

to reflesh the file

@mra-ruiz
Copy link

mra-ruiz commented Aug 25, 2022

For those using Z-Shell (zsh) don't forget it's .zshrc instead of .bash_profile

Thank you!! This worked for me too

@azadpsg
Copy link

azadpsg commented Jan 2, 2023

for fish terminal users use this

fish_add_path export '/home/$USER/.local/bin' 

@Thesecondbestname
Copy link

for fish terminal users use this

fish_add_path export '/home/$USER/.local/bin' 

Ok but how does this work? I'm not on my computer rn so I can't check, I am very new and have never seen a fish config file. Could you please explain how this works? Especially because I've seen the path to lunarvim in every other solution.

@aristotelesbr
Copy link

aristotelesbr commented Feb 21, 2023

Create an alias and happy coding.
In ~/.config/fish/config.fish add:

alias lvim="~/.local/bin/lvim"

But, to use correctly export from fish use:

fish_add_path -m ~/.local/bin

@oxido-std
Copy link

Thank you. I'm new in linux and thats was driving me crazy.

@XEROGAMINN13
Copy link

termux: cp .local/bin/lvim ../usr/bin/

@tinyBrained
Copy link

Hi i am very new to this, so the issues i have right now is, my college has blocked settings and control panel all together and so i am not able to flutter sdk to path using this method, is there anyother way i could add it to path?

@tim-247
Copy link

tim-247 commented Oct 20, 2023

On linux/macos can you use ~ in your PATH variable or do you have to use the full path to your home folder?

@jaimeleo8821
Copy link

Thanks, I can also edit the /etc/profile to add directory to $PATH and make it permanently.

@dwayneheasley
Copy link

worked perfectly, many thanks
@getaway shootout

@ShengzheXu
Copy link

For those using Z-Shell (zsh) don't forget it's .zshrc instead of .bash_profile

Hero! Thanks.

@correiamath
Copy link

correiamath commented Feb 3, 2024

SUMMARY

(For who came from LunarVim doc)

Step 1: Open your .bashrc file

Use a text editor of your choice to open the .bashrc file. In this example, I'll use the nano editor:

nano /home/your-user-name/.bashrc

Step 2: Add the directory to the PATH

In the opened .bashrc file, go to the last line and add the following:

export PATH=/home/your-user-name/.local/bin:$PATH

Step 3: Save and exit

After adding the line to the file, save your changes by pressing Ctrl + O, then press Enter. To exit the editor, press Ctrl + X.

Now, the specified directory is added to your PATH. This means that executable programs in the specified directory can be executed from any location in the terminal without needing to provide the full path.

@dnice1987
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment