Call script shell from another script shell via sudo - environment variables?

I call bash script B from script A. In script A (parent script) I export some variables. I would like to use these variables in script B (index), but the variable values ​​are not passed from script A to script B. Is there a way to access the variable values ​​from script B

#!/bin/bash
# script_A.sh
export VAR="value"
enter code here
sudo -u user ./script_B.sh

#!/bin/bash
# script_B.sh
echo $VAR    # this prints nothing
+3
source share
3 answers

As @geekosaur noted, sudodumps the environment for security reasons. To save the environment transition, -Ego to sudo.

from sudomanpage:

-E

-E ( ) ,                     . , -E,                     .

+8

script, sudo , , , . . man sudoers /etc/sudoers ( ) , .

+3

( ) , sudo:

$ cat test.sh 
#!/usr/bin/env bash
echo "$foo"
$ unset foo
$ foo=bar ./test.sh
bar
$ sudo foo=bar ./test.sh
bar
0

All Articles