0

shell_exec is not running on linux terminal

try to run below commands

[root@localhost conf]# shell_exec
-bash: shell_exec: command not found
[root@localhost conf]# shell_exec("pwd")
-bash: syntax error near unexpected token `"pwd"'
[root@localhost conf]#
4
  • 1
    shell_exec is a PHP function, why are you trying to run it without PHP? Commented Jun 27, 2022 at 3:38
  • shell_exec is not running from PHP also Commented Jun 27, 2022 at 3:47
  • If your question is about PHP, please post what you tried, what you expected the results to be, and what the outcome was, including any error messages. If your question is about Linux, there are other forums that might be better suited. Commented Jun 27, 2022 at 3:53
  • You can’t write one language (php) into another language interpreter (bash) and expect it to just work. It’s immaterial to bash what the string is it’s simply invalid bash commands/syntax Commented Jun 27, 2022 at 6:13

1 Answer 1

1

You're trying to execute PHP directly on the command line without using the PHP packaging, so the bash shell is attempting to execute it and doesn't recognize the PHP function.

You need to run shell_exec() in a PHP file or use the appropriate PHP CLI syntax. For example, you can pass -r to the php command, which allows you to run code.

myusername:~$ php -r 'echo shell_exec("pwd");'
/home/myusername
myusername:~$ 

Or you can use the built-in REPL.

myusername:~$ php -a
Interactive shell

php > echo shell_exec('pwd');
/home/myusername
php > 

Also note the use of echo here since the successful result will return a string that we want to see.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.