48

I accidentally unset all the directories of $PATH while trying to add a new one in ~/.bashrc. I opened a new terminal window as I was editing and now $PATH is empty. I'm worried if I boot from another drive to find the $PATH I won't be able to boot into this drive again.

Basically, what is the default result of echo $PATH?

3
  • 2
    Comment out or fix that export line and you should e good to go.
    – Panther
    Mar 16, 2012 at 5:28
  • 1
    ~/.bashrc is the wrong place to set environment variables though. You should do that in ~/.profile instead.
    – geirha
    Mar 16, 2012 at 6:38
  • Yes I figured it out. PATH is still fine in the other terminal window that was open before the new one, so just fixing the export line by adding :$PATH at the end restored the PATH. And yes in ~/.profile there is code to "set PATH so it includes user's private bin if it exists" i.e. $HOME/bin
    – Cee
    Mar 16, 2012 at 8:24

4 Answers 4

51

The answer to your question is:

PATH=$(getconf PATH)

and works on any POSIX compliant system. The selected answer is the correct way to augment the path without obliterating prior existing content. If you use bash, you might consider:

PATH+=:$mynewdir
7
  • 1
    I had trouble using a lot of commands (sed: No such file or directory type of trouble) after accidentally setting my PATH to nothing during a very long running process (i.e., I couldn't restart). I couldn't use getconf. To reset my path, I used $(export $(cat /etc/environment)":/usr/bin/additional:/usr/bin/paths"). For the curious: /etc/environment is where the PATH variable is initially set in many Linux flavours.
    – Nick Bull
    Aug 18, 2015 at 14:28
  • 2
    That working would surprise me. Perhaps "export" without the surrounding "$(...)" stuff might, but it will be whatever happens to be there. Use "source /etc/..". Anyway, try "/usr/bin/getconf" if you cannot even find "getconf".
    – Bruce
    Aug 31, 2015 at 21:56
  • Prefer your final idea using the full path a lot more than mine in hindsight! Thanks for the reply
    – Nick Bull
    Sep 1, 2015 at 9:05
  • 3
    If the PATH variable is empty, then PATH=$(getconf PATH) will not be useful but only result in bash: getconf: No such file or directory
    – Max N
    May 31, 2018 at 16:20
  • 1
    If PATH is empty, then note the comment above: try "/usr/bin/getconf". If that does not work, then you need to learn where your system stashes its POSIX commands and use that directory.
    – Bruce
    Jun 1, 2018 at 17:06
29

You can find it on /etc/environment:

$ /usr/bin/cat /etc/environment

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

So, just source it:

$ source /etc/environment 
$ echo $PATH

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
0
11

Adding :$PATH to the end of the export line fixed the problem e.g. export PATH=<directory to be added>:$PATH

I add this line to the ~/.bash_rc file instead of the ~/.profile file so I can see the effect immediately in a new terminal and for other reasons based on the information here: https://superuser.com/questions/176404/linux-bash-not-loading-profile-in-new-session

For me, the default output of echo $PATH before adding the new directory is:

/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
4

The default path is

/home/_username_/bin:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games 

Hope this helps you

1
  • That differs from my default PATH. See below.
    – Cee
    Mar 16, 2012 at 16:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .