Home > Mobile >  Adding stuffs to PATH in Linux
Adding stuffs to PATH in Linux

Time:06-09

I have recently started off with Linux, and am having some confusion with how to set something to PATH, I went through many wikis, but things aren't getting cleared, like I wanted to know what is the difference between putting things into the .bashrc file and /etc/environment file, and do we put the directory paths into the .bashrc file only, or is there some other file which we can use to mention our environment variables?

CodePudding user response:

Your options are: /etc/bashrc, /etcprofile, ~/.bashrc, ~/.profile`

The first two are generally not to be used as they are subject to change on system updates. If you must set global paths and environment (i.e. for every user) use /etc/bashrc.local or /etc/profile.local .

If you want to set env. variables for yourself/one user only then use ~/.bashrc or ~/.profile. ~/.profile is read for a login-shell only (according to the comments in mine:

# This file is read each time a login shell is started.
# All other interactive shells will only read .bashrc; this is particularly
# important for language settings, see below.

So shells you start by clicking on a Terminal icon will read ~/.bashrc.

As a rule I tend to put my global PATH modifications (i.e. for CUDA) into /etc/bashrc.local and anything that is only for me into ~/.bashrc.

Modifying PATH (and others) should be done by prepending to it via: export PATH=<path>/<to>/<new SW binaries>:$PATH

CodePudding user response:

I wanted to know what is the difference between putting things into the .bashrc file and /etc/environment file,

Linux is a multi-user system. The same Linux computer (e.g. laptop) can be used by you and by your partner. At 10am you are using it, at 3pm you give your laptop to your partner and he would use it (with a different login). And you can configure your laptop to hide your files to your partner.

Linux workstations can be used by several users (human persons) at once. For example, you can buy a Linux desktop, and plug two screens, two keyboards, two mouses into it, and configure all that to have two different persons using it at once.

You could even configure your laptop so that your colleague is accessing your laptop A from his/her laptop B with ssh.

With that in mind:

  • /etc/environment is (like every other file under /etc/) setting something globally (for users using GNU bash)

  • your $HOME/.bashrc (e.g. /home/peter/.bashrc if your $HOME is /home/peter) is specific to you. Your colleague John (accessing the same computer) will use the settings in /home/john/.bashrc

  • John could have a different login shell (e.g. zsh). Then his settings are in /home/john/.zshrc and other files.

  • Most supercomputers (costing 10M€ or 10MUS$) are running Linux, and used by hundreds of different persons (sitting in different offices, or even cities) at the same time.

Because of setuid techniques, many Linux distributions are using (internally) other "users" to restrict access to services like databases. See syscalls(2) and execve(2) and elf(5).

The $PATH is explained in environ(7) and used by functions like exec(3) (that most Unix shells are calling). A lot of Linux shells are open source (e.g. GNU bash or zsh) and you could study their source code (and improve it), or code your own shell. I advise to have a short $PATH.

  • Related