14

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/"
6
  • For background on this part of .profile, see askubuntu.com/questions/284640 .
    – JdeBP
    Commented Jul 23, 2017 at 11:02
  • Which shell is this?
    – njsg
    Commented Jul 23, 2017 at 11:18
  • 2
    I assume the directory exists, right?
    – Beat Bolli
    Commented Jul 23, 2017 at 11:21
  • @njsg it is bash
    – user106035
    Commented Jul 23, 2017 at 11:34
  • Possibly something else is resetting your PATH variable after this is run. You can verify by using the -x flag passed to bash and seeing where the PATH is reset. Also, you need to export PATH="$HOME/bin:$PATH" to provide it to subsequent processes. Commented Jul 23, 2017 at 22:35

3 Answers 3

11

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.

5
  • 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.
    – JdeBP
    Commented Jul 23, 2017 at 11:01
  • @JdeBP : added clarification to answer. Assumed as a Debian user they had not changed their default interactive shell, but I guess they could have.
    – Tigger
    Commented Jul 23, 2017 at 11:05
  • I don't have any of the ~/.bash_profile nor ~/.bash_login. At my ~ there are only .bash_history and .bash_logout (and of course the .profile).
    – user106035
    Commented Jul 23, 2017 at 12:31
  • @Tigger updated!
    – user106035
    Commented Jul 24, 2017 at 10:25
  • @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.
    – Tigger
    Commented Jul 24, 2017 at 10:58
6

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.)

4
  • sourcing .profile from .bashrc ! in most of cases .profile contains ` . .bashrc` , this won't go trought an infinite loop ?!
    – Yunus
    Commented Jul 23, 2017 at 14:31
  • @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. Commented Jul 23, 2017 at 15:04
  • 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 :-)
    – Yunus
    Commented Jul 23, 2017 at 16:16
  • 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.
    – user232326
    Commented Jul 24, 2017 at 13:13
1

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"

You must log in to answer this question.