3

When I type echo $PATH, I get the output:

/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games

However, I modified my PATH variable using gksudo gedit /etc/environment; the file now reads PATH="/opt/texbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games".

I am not sure why I am getting two different value of PATH. How can I fix it?

I am running Xfce on a Chromebook using Crouton.

14
  • What command did you use to modify your PATH variable? Commented Apr 16, 2015 at 21:38
  • @cremefraiche I used <code> gksudo gedit /etc/environment </code> and then added <code> "/opt/texbin"</code> saved it rebooted but still the same problem.
    – DBS
    Commented Apr 16, 2015 at 21:40
  • 7
    That is not the way you should be changing your path variable. If you are trying to change the PATH variable for the current user only, modify the line in ~/.bashrc that says export PATH=$PATH:.... All you need to do is add a colon to the end and include the path. Example: export PATH=$PATH:...:/new/path Commented Apr 16, 2015 at 21:44
  • @cremefraiche thanks let me try that. Btw would you explain why editing the PATh in /etc/environment doesnt work?
    – DBS
    Commented Apr 16, 2015 at 21:46
  • 2
    From what I understand it is best not to mess with that file directly, in order to minimize risk of breakage. I should mention that ~/.bashrc is run every time an interactive shell is opened, while ~/.bash_profile is run when a login shell is opened. Pick which file best suits your needs. Commented Apr 16, 2015 at 21:54

4 Answers 4

3

Edit

  • /etc/profile to affect all users.
  • ~/.bash_profile to affect single users bash shell (so not this one, as it is for bash specific stuff).
  • ~/.profile to affect single user, all shells.

Note: If you have both .profile and .bash_profile and you want both to be read by bash, then you will have to add . .profile to your .bash_profile, as .profile is not read by default, if .bash_profile exists

on sudo

  • don't run your editor as root, so don't do gksudo gedit «filename»
  • Avoid running X11 apps as root, so don't do gksudo gedit «filename»
  • instead do EDITOR=gedit sudoedit «filename». It will run the editor as you on a temporary file, it will copy the fill as root when you finish.
0
1

The correct place to set your PATH is in ~/.bash_profile:

PATH="$PATH:/some/extra/paths:/may/go/here"

The PATH variable should already be exported, so you shouldn't need to export it again (exporting it again has no further effect).

The default path for bash is hardcoded into the bash executable, and then further (possibly) modified in /etc/profile and (on some systems) /etc/bash.bashrc.

My guess is that the path set in /etc/environment is primarily used by non-shells, such as cron etc.

See also:

1
  • The last path, /etc/bash.bashrc is actually set in the macro SYS_BASHRC and can be changed at compile time. For example, Artix Linux changes it to /etc/bash/bashrc. Commented Feb 12, 2024 at 10:32
0

/etc/environment is the system wide fallback. The per-user config in ~/.bash_profile probably overrides it. Regardless, don't mess with /etc/environment for configuring bash, instead put whatever PATH you want in ~/.bash_profile. Even if /etc/environment is wrong it will be overridden by what you put in ~/.bash_profile.

You should probably also restore /etc/environment to what it was before to avoid minor breakage with programs that depend on it. That file comes with the package, so you can use your package manager to reset it to its original form.

0

update note: I wrote this for zsh but it works for bash as well.

Here is a trick I use to keep my path clean. (Note: I load in zsh configs as separate files. This file needs to load first so an easy way to force that while maintaining standardized naming conventions is to use prefixes. Prefixing with 0_pathfix.zsh will cause this to run before the other Zsh config files.)

# add to path if $1 is not already in path
pathAppend() {

  if ! echo $PATH | egrep -q "(^|:)$1($|:)" ; then
    PATH=$PATH:$1
  fi
}

# filters the path checking against itself for duplicates returns clean path then exports the clean path. 

PATH=$(echo "$PATH" | awk -v RS=':' -v ORS=":" '!a[$1]++{if (NR > 1) printf ORS; printf $a[$1]}'); export PATH;

# This ensures that even if you manually added to the path the next time zsh source is reloaded the path will clean itself.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.