Is there any way for me to set an env variable without editing files to be in memory and effect other shells as well, which will take effect on any new user who connects that machine and will take effect only after next reboot of the Linux machine?

I'm trying to remember something in my linux which will be effective (from a script) only for the time the machine is up (I don't want a file for that reason) so from my shell I want to set some flag which will be valid for any user connecting like:


however I want it to be effective for any new shell opened on that Linux. and I want that next time someone reboots that machine that variable disappears...

Any (simple) for me to achieve that? (without using files...)

migrated from stackoverflow.com Apr 5 '11 at 14:21

This question came from our site for professional and enthusiast programmers.


Well, the files in /tmp are customarily cleaned on reboot, so why not have some script source a file there, something along the lines of "if this file exists, source it"? Then, you can do your initialization in that file as necessary. This could be put in some bash initialization file that is sourced by login in shells like /etc/profile. (This assumes you want it to be sourced by interactive shells rather than login shells.)

However, I'm not really clear about the context. If you could give more details about what you are trying to do, you would get better answers.


You can use export to mark a variable to be passed to child processes in the environment.

export linux_test_run=yes

If you want the variable to be available to newly connected users you'll need to edit the shell's initialization files.

  • Technically true, but I don't see how that would help, since the change is supposed to take effect only after the next reboot. – Gilles Apr 5 '11 at 20:07

Sorry, you'll have to edit a startup file somewhere, and if you want it for all users, probably the bashrc in /etc.

However, if the only reason that you don't want to use files is that you want this behaviour to disappear on reboot, there are other ways to do that:

  1. Call a file setvariable.bash from within the bashrc. Within that file, you export your variable
  2. Put a file in your /etc/init.d that will launch on reboot and deactivate the setvariable.bash file (replacing it by a blank file or something)

It's a hack, but it might work.

  • bashrc is a bash configuration file, it's not a place for environment variables. /etc/profile is the most common place for environment variables; most systems have other possibilities such as /etc/environment. – Gilles Apr 5 '11 at 20:06

Most login methods read environment variables from /etc/profile. (There may be other possibilities but this is the easiest to act on.) So if I understand you correctly, you'd like to add an environment variable assignment to /etc/profile on the next reboot, and remove it on the reboot after that.

First, if you don't have that already (some distribution set it up), create a directory called /etc/profile.d and add this snippet to /etc/profile:

for x in /etc/profile.d/*.sh; do
  . "$x"
unset x

Create a file containing export linux_test_run=yes called /etc/profile.d/test_run.sh.0.

Add the following snippet to /etc/rc.local (or some other script that's executed at boot time):

for x in /etc/profile.d/*.sh.[0-9]*; do
  n=${x##*.}              # extract the part after the last .
  case n in
    *[!0-9]*) continue;;  # n is not a number, so skip this file
    *[1-9]*) mv "$x" "${x%.*}.$(($n-1))";; # decrement the number in the file name
    *)     # the file is *.sh.0, so rename to .sh and schedule for deletion
      echo "${x%.*}" >>/etc/profile.d/cancel-on-reboot.list
      mv "$x" "${x%.*}";;
if [ -e /etc/profile.d/cancel-on-reboot.list ]; then
  # rename all files in this list so they won't be considered any longer
  while read -r x; do
    mv "$x" "$x.cancelled"
  done </etc/profile.d/cancel-on-reboot.list
  rm /etc/profile.d/cancel-on-reboot.list

If you'd like to reenable the setting for one reboot, rename it to *.sh.0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.