How to add /home/username/bin to $PATH?

180,282

Solution 1

To do that you need to type in your terminal:

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

This change is only temporary (it works only in the current session of the shell). To make it permanent, add the line to your .bashrc file located in your home directory.

Solution 2

Ubuntu (and Debian based distros) automatically add $HOME/bin to the PATH if that directory is present. You can check this in ~/.profile:

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi
Share:
180,282

Related videos on Youtube

user233233
Author by

user233233

Updated on September 18, 2022

Comments

  • user233233
    user233233 over 1 year

    Show how you can add /home/<yourusername>/bin to the $PATH variable. Use $HOME (or ~) to represent your home directory.

    • WinEunuuchs2Unix
      WinEunuuchs2Unix almost 6 years
      Reopen Voters /home/<yourusername>/bin is a Special directory that gets automatically added to the $PATH after it's been created and ~/.profile is reloaded. The duplicate target is about adding generic directories to the path such as /mary/had/a/little/lamb.
    • muru
      muru almost 6 years
      @WinEunuuchs2Unix So what? Why should this be reopened? Do the answers to the dupe no longer apply? In fact, the accepted answer to the dupe mentions this very directory, and provides the same snippet that's in the default ~/.profile!
    • WinEunuuchs2Unix
      WinEunuuchs2Unix almost 6 years
      @muru The "so what" is that you don't need to add /home/YOURNAME/bin to the$PATH. It's done automatically.
    • muru
      muru almost 6 years
      @WinEunuuchs2Unix again, does that mean the answers to the dupe can't be used?
    • muru
      muru almost 6 years
      Reopen Voters: The only "special" part is that after you create this directory and start a login shell (or source ~/.profile), this gets added to the PATH. For all other cases, the answers to the dupe will have to be used. This is a dupe.
    • WinEunuuchs2Unix
      WinEunuuchs2Unix almost 6 years
      Yes the dupe handles adding a directory to the path. This question is explicit about /home/YOURNAME/bin which means you don't want to add it to the path. This could account for duplicates mentioned in comments by @sdaffa23fdsf like the accepted answer here might cause. I've asked @sdaffa23fdsf for documented examples of multiple instances of ~/bin in the path. This question could almost be thought of as "What directories do you NOT WANT TO ADD TO THE PATH".
  • Panther
    Panther over 10 years
    I would use /home/user_name rather then $HOME
  • Swordfish90
    Swordfish90 over 10 years
    It is the same. If you try "echo $HOME" you will probably see the folder /home/user_name...
  • Panther
    Panther over 10 years
    $HOME is a variable and is thus ambiguous. IMO it is best to use the full path in scripts and when adding to your $PATH
  • rubo77
    rubo77 almost 10 years
    In case ~/.profile is not loaded add this to your ~.bashrc: PATH="$HOME/bin:$PATH"
  • Gauthier
    Gauthier almost 7 years
    @bodhi.zazen Your HOME is not guaranteed to by at the same location on different systems. For example, I use the same .bashrc on Linux and MacOS, and hard-coding the full path would not work.
  • WinEunuuchs2Unix
    WinEunuuchs2Unix almost 6 years
    @sdaffa23fdsf Do you have a documented example of the multiple ~/bin? In that case the ~/.profile script should be changed to check if ~/bin is already in the path before prepending it to $PATH.
  • Polv
    Polv about 3 years
    I actually need to create this file in Arch.
  • Admin
    Admin almost 2 years
    When exactly does ~/.profile get sourced? I used to think that sourcing the ~/.bashrc file with . ~/.bashrc also sources the ~/.profile file, but I just proved to myself that is not the case! Rather, it's the other way around!: sourcing the ~/.profile file with . ~/.profile actually sources the ~/.bashrc file as well, so long as the default Ubuntu ~/.profile file is in-use, since it contains lines to do so if running bash.
  • Admin
    Admin almost 2 years
    Note also that on Linux Ubuntu, a backup copy of your default ~/.profile file resides in /etc/skel/.profile. Other default files are in the /etc/skel/ directory too.