1

How would you get the string output of the current shell in PHP?

This is what I tried

$output = shell_exec('echo $SHELL 2>&1');

but I'm getting empty output when I print it out.

$output should be a string that is something like /bin/bash

user3226932
  • 2,042
  • 6
  • 39
  • 76
  • Possible duplicate of [shell\_exec() returning null on "ls"](http://stackoverflow.com/questions/18241305/shell-exec-returning-null-on-ls) – l'L'l Apr 28 '17 at 22:15
  • 1
    The code above produces no errors for me and runs just fine, returning the value I expect. I'd suggest simplifying the script include just this line along with the opening php tag and checking out the error log to see if something else failed in php which might cause nothing to be output. – Jonathan Kuhn Apr 28 '17 at 22:21
  • 1
    @l'L'l the only (visible) anwser on that question says to add `2>&1` to the command, which OP already has. It is not a duplicate for that question. – Jonathan Kuhn Apr 28 '17 at 22:22
  • @JonathanKuhn: The permissions were more of what might be worth focusing on... – l'L'l Apr 28 '17 at 22:34
  • @user3226932: Do other commands return output normally? – l'L'l Apr 28 '17 at 22:37
  • yup, maybe I don't have permissions to use environmental variable or $SHELL is actually empty, even though when I do echo $SHELL it's there – user3226932 Apr 28 '17 at 22:41
  • `PHP` is generally is handled as a different user:group than you, so it's quite plausible that's the underlying reason. In your `php.ini` search for `disable_functions =`... if it's not already, try and comment it out `; disable_functions = ...`, restart apache/php and see if it works; safemode should be off. – l'L'l Apr 28 '17 at 23:53

1 Answers1

1

There is an specific function for getting environment variable values that you can use: getenv():

echo getenv('SHELL');
sidyll
  • 57,726
  • 14
  • 108
  • 151