Environment variable can not be exported in /etc/profile.d/script.sh

Export new environment variable does not work in a new script from /etc/profile.d/ for all users.

Please you can test, if it works: :point_down:
For example:

  1. Create a new script sudo touch /etc/profile.d/keep-xdg.sh.
  2. Edit this script:
#!/bin/sh
export DO_NOT_OVERRIDE_XDG_CURRENT_DESKTOP=1
  1. Make it as the executable file: sudo chmod +x /etc/profile.d/keep-xdg.sh
  2. KDE autostart adds this script. (Optional)
  3. Reboot or logout then login
  4. Check if the env variable exists:
    printenv | grep DO_NOT_OVERRIDE_XDG_CURRENT_DESKTOP
    but it does not exist. ----> That is the issue.

~/.bashrc for bash or ~/.zshrc for zsh works fine with export env variable, but it is for individual user.
I want export env variable for all users, not individual user.

Do you have idea for solution?
How to set permanent global environment variables for all users?

1 Like

Remove the hashbang line, i.e. #!/bin/sh. If you leave it in, then whatever code is in the file will be run in a subshell, and you cannot export a variable from a subshell to its own parent. You can only export variables and functions from the current environment into subshells thereof.


P.S.: None of the files in /etc/profile.d need to have the execute bit (+x) set for anyone, because they are sourced into the running environment, not executed. :arrow_down:

sudo chmod 644 /etc/profile.d/*
4 Likes

Thank for the answer. You are right.
But how can I check if env variable exists for all users?

I think printenv or env is for individual user. I can not see the env variable DO_NOT_OVERRIDE_XDG_CURRENT_DESKTOP exists.

It will, because those files are sourced every time someone logs in. Every login session reads the same startup files.

P.S.: You will have to log out and back in after changing the file. The files under /etc/profile.d are read only once upon each login.

3 Likes

No they dont.
This is true for things like /etc/environment and /etc/profile
But not true for their HOME counterparts such as ~/.profile

The way to get ‘other users’ env vars is probably not through some single administrative command, but your regular env initiated as the given user, ie:

sudo -Hiu $USER env

I know that, and I wasn’t suggesting that every login session reads the shell initialization files in every user’s home directory. I was clearly only talking about the systemwide files.

Sorry, that did not seem clear to me. Quite the opposite.

It still looks that way to me. But if you meant only the system files then theres nothing incorrect I guess :woman_shrugging:

Only /etc/environment works fine with export of the env variable for all users, which I can see with env | grep XXX that it shows value.

/etc/profile and /etc/profile.d/xx.sh can not export it to env. It is read-only.

I guess I will just leave this here…

https://wiki.archlinux.org/title/Environment_variables

1 Like

I have checked a lot, that /etc/profile.d/xx.sh works randomly after reboot/login and exports the env variables or not. This is a new bug! :bug: /etc/profile too.

Reboot or login won’t help, but source /etc/profile.d/xx.sh can help.

I finally found the problem that the fish shell is not able to run POSIX-compatible scripts like /etc/profile.

Solution:
Switch back to bash or zsh to solve this problem.
chsh -s /bin/bash or chsh -s /bin/zsh for root and user.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.