$HOME/bin dir is not on the $PATH

19,828

Solution 1

From the top of ~/.profile:

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

So (if you are using bash as your shell) I'm guessing either ~/.bash_profile or ~/.bash_login is on your system. Select one and edit it to include:

export PATH=$PATH:$HOME/bin

Then save and source ~/.bash_login or logout and log back in.

Edit:

You say that both ~/.bash_profile and ~/.bash_login are both missing from your $HOME. I think we need confirm a few things. Please post the results of the following in your original question:

echo $0
echo $HOME
whoami
less /etc/*-release

Edit 2:

Personally, I do not know why ~/.profile is not being included in your case based on the information provided and documentation. While testing I did notice that my ~/.profile is scanned when I ssh in but not when I launch a new terminal.

But, there is a simple solution to allow $HOME/bin to be included in your interactive shell. Edit (create if not present) ~/.bashrc and add the following line to it:

export PATH=$PATH:$HOME/bin

Save, logout and log back in, or source ~/.bashrc.

The export line could be expanded to check that $HOME/bin exists if you like with:

if [ -d "$HOME/bin" ]
then
    export PATH=$PATH:$HOME/bin
fi

Why ~/.bashrc instead of another file? Personally preference and seems to be more reliable too.

Solution 2

The rules for in-sourcing shell startup files are complex. It's likely that with your setup, .profile is not getting included when you open up a new terminal within an X session (try putting an echo .profile inside of .profile and see if the message shows up when you start a shell).

.  "$HOME/.profile"

should reload the profile manually.

Logging in and out of X should also cause .profile to load.

Alternatively, you can do . $HOME/.profile from .bashrc (while using a variable-based guard to prevent double inclusion) to make sure .profile is always included whenever you start a shell.

(You shouldn't need to export PATH as PATH is already an exported variable and modifying its value won't change its export status.)

Solution 3

If you want to get .profile loaded all you need is to start a login shell:

$ bash -l

That should be enough for a running session. You can compare the PATH before and after a login bash has been started to confirm the difference.

For a more permanent solution you need that a login shell to be started at some point before your terminal (console) starts. The login as an specific user happens on some dm (display manager) (gnome,kde,xfce,lxde, etc). It should be the job of any of them to change the environment variable PATH to match your needs.

For example, for xfce, the solution is to change xinitrc:

$ cat >"$HOME/.config/xfce4/xinitrc" <<-\_EOT_
#!/bin/sh

# Ensure programs in ~/bin are available for the X session.
p="$HOME/bin";
[ "$p" != "${PATH%%:*}" ] && export PATH=$p:$PATH
_EOT_
cat "/etc/xdg/xfce4/xinitrc" | tail -n+2 >> "$HOME/.config/xfce4/xinitrc"

For gnome, it seems that the file to change is ~/.pam_environment.

And for KDE, follow this guide, to create the file, you may use this code:

$ file='$HOME/.config/plasma-workspace/env/path.sh'
$ code='export PATH=$HOME/bin:$PATH'
$ echo "$code" >> "$file"
Share:
19,828

Related videos on Youtube

Chris Tang
Author by

Chris Tang

Updated on September 18, 2022

Comments

  • Chris Tang
    Chris Tang over 1 year

    In my ~/.profile I have a last block which should load my personal bin/ directory like this:

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

    But it is seemingly not loaded:

    echo $PATH
    /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
    

    Why is this not working? (My shell is bash.)

    Edit for Tigger

    echo $0 => bash
    
    echo $HOME => /home/student
    
    whoami => student
    
    less /etc/*-release => 
    PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
    NAME="Debian GNU/Linux"
    VERSION_ID="9"
    VERSION="9 (stretch)"
    ID=debian
    HOME_URL="https://www.debian.org/"
    SUPPORT_URL="https://www.debian.org/support"
    BUG_REPORT_URL="https://bugs.debian.org/"
    
    • JdeBP
      JdeBP almost 7 years
      For background on this part of .profile, see askubuntu.com/questions/284640 .
    • kmacdonald
      kmacdonald almost 7 years
      Which shell is this?
    • Beat Bolli
      Beat Bolli almost 7 years
      I assume the directory exists, right?
    • Admin
      Admin almost 7 years
      @njsg it is bash
    • Charles D Pantoga
      Charles D Pantoga almost 7 years
      Also you should use the bashrc file if you only need this loaded for interactive sessions.
  • JdeBP
    JdeBP almost 7 years
    Although, since the questioner did not specify which shell, there is also the possibility that one is not using the Bourne Again shell at all. Certainly other people with this same question might not be.
  • Tigger
    Tigger almost 7 years
    @JdeBP : added clarification to answer. Assumed as a Debian user they had not changed their default interactive shell, but I guess they could have.
  • Yunus
    Yunus almost 7 years
    sourcing .profile from .bashrc ! in most of cases .profile contains ` . .bashrc` , this won't go trought an infinite loop ?!
  • PSkocik
    PSkocik almost 7 years
    @youness I'm doing both and I'm using include guards ([ -z "$has___profile" ] || return; has__profile=1) so I get no infinite loops and both .profile and .bashrc (only if BASH_VERSION is defined) no matter how I got there.
  • Yunus
    Yunus almost 7 years
    i got it ! my bad english pushed me to ask this uneeded question , ( puting code for explaination is better than literal expression ) . thanks for your time :-)
  • Admin
    Admin almost 7 years
    @Tigger updated!
  • Tigger
    Tigger almost 7 years
    @student : Made another update. Really at a loss as to what is going on. Pretty interested to hear if you find out why ~/.profile is being skipped.
  • done
    done almost 7 years
    A simple bash -l will load .profile (unless some odd configuration). Also, the PATH should be correctly set when the user logs in. That means in the "display manager" (dm) in use: gnome, KDE, xfce, lxde, etc.