3

How I may check root environment variables from sudo user? I tried various keys and combinations of su and sudo, but have no luck.

For example, $HISTFILESIZE=2000 for ordinary user and 9999 for root user.

vagrant@default-debian-78-64-nocm:~$ echo $HISTFILESIZE
2000
vagrant@default-debian-78-64-nocm:~$ sudo -i
root@default-debian-78-64-nocm:~# echo $HISTFILESIZE
9999

But when I try to get root $HISTFILESIZE via sudo - it always returns me a 2000:

vagrant@default-debian-78-64-nocm:~$ sudo -i echo $HISTFILESIZE
2000
vagrant@default-debian-78-64-nocm:~$ sudo -i su - -c "echo $HISTFILESIZE"
2000
vagrant@default-debian-78-64-nocm:~$ sudo -i bash -c "echo $HISTFILESIZE"
2000
vagrant@default-debian-78-64-nocm:~$ sudo -i bash -l -c "echo $HISTFILESIZE"
2000

1 Answer 1

5

Your shell will expand the variable before it even reaches sudo. Instead of

sudo -i echo $HISTFILESIZE

and all the variations, you need to escape it with a \ as below:

sudo -i bash -c "echo \${HISTFILESIZE}"

One more edit: need to bracket variable, it may contain spaces

2
  • Not working, it returns '$HISTFILESIZE' string instead of it value
    – strangeman
    Sep 11, 2015 at 3:22
  • 1
    UPD: sudo -i bash -c "echo \$HISTFILESIZE" working correctly, thanks!
    – strangeman
    Sep 11, 2015 at 3:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .