[Solved] Difference between /etc/environment, .bashrc and .profile

Linux Kernel, Network, and Services configuration.
Post Reply
Message
Author
DebianFox
Posts: 191
Joined: 2024-05-05 14:11
Has thanked: 23 times
Been thanked: 15 times

[Solved] Difference between /etc/environment, .bashrc and .profile

#1 Post by DebianFox »

What are the main differences between the following? And when should environment variables be put in each of them?
1) /etc/environment
2) $HOME/.bashrc
3) $HOME/.profile

Also if a Desktop environment (KDE or GNOME ) or Window Manager (IceWM, XFCe, etc) is used and an application is started using a shortcut then which of these files is parsed and then the variables used?
Last edited by DebianFox on 2024-09-02 09:52, edited 1 time in total.

User avatar
Bloom
df -h | grep > 90TiB
df -h | grep > 90TiB
Posts: 530
Joined: 2017-11-11 12:23
Been thanked: 34 times

Re: Difference between /etc/environment, .bashrc and .profile

#2 Post by Bloom »

/etc/ configuration files affect the whole system. Configuration files in your home directory affect only you as a user, no other users and not the root.
You don't need to edit them or add to them, unless you know what you're doing.

User avatar
wizard10000
Global Moderator
Global Moderator
Posts: 1144
Joined: 2019-04-16 23:15
Location: southeastern us
Has thanked: 120 times
Been thanked: 198 times

Re: Difference between /etc/environment, .bashrc and .profile

#3 Post by wizard10000 »

All of those files are parsed. As @Bloom mentioned /etc/environment is a systemwide config. ~/.profile is sourced once when you first log on to the machine, ~/.bashrc is sourced any time bash is called - like when you run a script or open a terminal window.

Hope this helps-
we see things not as they are, but as we are.
-- anais nin

DebianFox
Posts: 191
Joined: 2024-05-05 14:11
Has thanked: 23 times
Been thanked: 15 times

Re: Difference between /etc/environment, .bashrc and .profile

#4 Post by DebianFox »

Thanks that does help. So if I want a environment variable to be defined, like those observed when we give the env command in the terminal, for all the users then those variables have to be put in /etc/environment file. However if I want a particular variable to be defined only for a particular user, then should that be put in .profile file or .bashrc file?

For example there is an application that is run by User A. The application is not started from a terminal shell, i.e. bash shell. Rather from a shortcut. This application looks for a particular environment variable say MYDIR. And I want this variable to be different for each user. So should I put this in .profile?

User avatar
wizard10000
Global Moderator
Global Moderator
Posts: 1144
Joined: 2019-04-16 23:15
Location: southeastern us
Has thanked: 120 times
Been thanked: 198 times

Re: Difference between /etc/environment, .bashrc and .profile

#5 Post by wizard10000 »

I'd stick it in .profile because the variable only needs to be set once. It's not gonna hurt anything to put it in .bashrc but it seems a little inefficient to reset the same variable every time bash is called.
we see things not as they are, but as we are.
-- anais nin

DebianFox
Posts: 191
Joined: 2024-05-05 14:11
Has thanked: 23 times
Been thanked: 15 times

Re: Difference between /etc/environment, .bashrc and .profile

#6 Post by DebianFox »

But the application is not launched via shell. A terminal/shell is not opened first.

User avatar
wizard10000
Global Moderator
Global Moderator
Posts: 1144
Joined: 2019-04-16 23:15
Location: southeastern us
Has thanked: 120 times
Been thanked: 198 times

Re: Difference between /etc/environment, .bashrc and .profile

#7 Post by wizard10000 »

DebianFox wrote: 2024-08-29 13:10But the application is not launched via shell. A terminal/shell is not opened first.
I think you just answered your own question :D
we see things not as they are, but as we are.
-- anais nin

DebianFox
Posts: 191
Joined: 2024-05-05 14:11
Has thanked: 23 times
Been thanked: 15 times

Re: Difference between /etc/environment, .bashrc and .profile

#8 Post by DebianFox »

So it is better to put the environment variable in ~/.profile file? If it is meant for one user only.

User avatar
wizard10000
Global Moderator
Global Moderator
Posts: 1144
Joined: 2019-04-16 23:15
Location: southeastern us
Has thanked: 120 times
Been thanked: 198 times

Re: Difference between /etc/environment, .bashrc and .profile

#9 Post by wizard10000 »

Yep. No reason to redefine those variables every time you open a terminal window :)
we see things not as they are, but as we are.
-- anais nin

Post Reply