I'm currently working on creating 2 bashes with two different scripts which result in two files named with the same timestamp for easy comparison/processing later. The date variable gets created in the first sh and I try to create an environment for it to get passed onto the second sh so that it can us the same timestamp. I'm using timeout because the second script is a looping process which inputs data to the file continuously until it is killed.


current_date_time="`date +%Y-%m-%d-%H:%M:%S`"
OUTPUT=$current_date_time --ouput-format csv
(export current_date_time; sudo timeout 15 2.sh)


echo "The variable is $current_date_time"
call loop

The first script functions normally while the second one only results in the phrase The variable is while the output file fails to get made. My suspicion is on timeout but any ideas would be appreciated.

  • 3
    Does your sudoers policy preserve arbitrary environment variables? what happens if you change the command to sudo -E timeout 15 2.sh? – steeldriver Apr 22 '19 at 0:28
  • @steeldriver Yea that seems to have been the oversight. It works as I hoped now thanks! – BurgerGru Apr 22 '19 at 0:43
  • @steeldriver, that comment sounds like The Answer; will you post it? Thank you! – Jeff Schaller Apr 22 '19 at 1:00
  • @JeffSchaller I've added a brief answer below - TBH I'm not entirely familiar with the details – steeldriver Apr 22 '19 at 1:25

As noted in man sudoers:

   Command environment
     Since environment variables can influence program behavior, sudoers pro‐
     vides a means to restrict which variables from the user's environment are
     inherited by the command to be run.  There are two distinct ways sudoers
     can deal with environment variables.

     By default, the env_reset option is enabled.  This causes commands to be
     executed with a new, minimal environment.

You can request for the calling environment to be preserved by invoking sudo with the -E or --preserve-env option:

sudo -E timeout 15 2.sh

(although this may be unsuccessful, if the sudoers policy forbids it).

  • 1
    It is a complex interplay of several configuration variables; unix.stackexchange.com/a/151315/117549 is one answer here, but the key parts to navigate are: env_reset, env_keep, env_check, and env_delete. It wouldn't hurt to have a good canonical question on sudo environment-variable behavior here. – Jeff Schaller Apr 22 '19 at 2:49

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.