2

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
4

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

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

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.