2

Currently, I have a script that loads my environment variables. And I'm sourcing it like this:

#!/bin/bash

set -x

# sourcing my env variables
. /etc/profile.d/company_env.sh > /dev/null 2>&1

clear_known_hosts(){
  local known_hosts="${NAGIOS_HOME}/.ssh/known_hosts"
  # clear
  su - nagios -c "echo '' > ${known_hosts}"

}

But, removing the sourcing part, because I don't want to add it in every script that may create, my sudo command can't find my environment variables, like NAGIOS_HOME for example, which is empty.

Any suggestions ?

Valter Silva
  • 16,446
  • 52
  • 137
  • 218

1 Answers1

3

sudo has option -E or long-option --preserve-env for this.

Usage: sudo -E command args ...

Of course, you would need to export the variable from parent shell, in order for it to be visible to sudo.

anishsane
  • 20,270
  • 5
  • 40
  • 73
  • Yeah, I know that option too. But I was trying to use sudo without it. I'm afraid that is not a good option, is it ? :) – Valter Silva Aug 17 '16 at 11:59